提出 #42102172


ソースコード 拡げる

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,n) for (int i=a;i<(int)(n);i++)
#define per(i,a,n) for (int i=n;i-->(int)(a);)
ll read(){ll r;scanf("%lld",&r);return r;}

class UnionFind{
  public:
    vector<int> f; // f[i]
    UnionFind(int sz){ // 0-index
      f.resize(sz);
      iota(begin(f),end(f),0);
    }
    int leader(int u) { return f[u] == u ? u :f[u] = leader(f[u]); }
    void merge(int u,int v) { f[leader(u)] = leader(v); }
    bool same(int u,int v) {return leader(u) == leader(v);}
};

class lowlink {
  int n;
  vector<vector<pair<int, int>>> G;
  vector<int> in, out, low;
  unordered_map<int, int> bridge; // bridge[eid] = child vertex

  void dfs(int u, int p, int &ti) { // 计算所有low,和找桥
    in[u] = low[u] = ti++;
    for (auto [v, id]: G[u]) {
      if (in[v] == -1) {
        dfs(v, id, ti);
        low[u] = min(low[u], low[v]);
        if (low[v] > in[u]) bridge[id] = v;
      } else if (id != p) {
        low[u] = min(low[u], in[v]);
      }
    }
    out[u] = ti;
  }

  void init() {
    n = G.size();
    in.assign(n, -1);
    out.assign(n, -1);
    low.assign(n, -1);
    int ti = 0;
    rep(i,0,n) if (in[i] == -1) dfs(i, -1, ti);
  }

  public:
  lowlink(const vector<vector<pair<int, int>>> &G) : G(G) { init(); }

  bool query(int a, int s, int t) { // a是桥 且 s,t有且只有一个在 桥子节点的子(树/图)中
    assert(0 <= s and s < n);
    assert(0 <= t and t < n);
    assert(s != t);
    if (!bridge.count(a)) return false;
    int l = in[bridge[a]];
    int r = out[bridge[a]];
    s = in[s];
    t = in[t];
    return (l <= s and s < r) xor (l <= t and t < r);
  }
};

int main() {
  int n=read();
  int m=read();
  vector<int> u(m), v(m);
  vector<vector<int>> w2e(m); //
  rep(i,0,m){
    u[i]=read()-1;
    v[i]=read()-1;
    w2e[read()-1].push_back(i);
  }
  int q=read();
  vector< vector<array<int,3> > > e2stq(m);
  rep(i,0,q) {
    int eid=read()-1;
    int s  =read()-1;
    int t  =read()-1;
    e2stq[eid].push_back({s,t,i});
  }
  vector<int> ans(q, -1);
  UnionFind uf(n);
  rep(w,0,m) { // 先处理 一定不可能0
    for(int eid:w2e[w]) for(auto [s,t,qid]:e2stq[eid]) if (uf.same(s, t)) ans[qid] = 0;
    for(int eid:w2e[w]) uf.merge(u[eid], v[eid]);
    for(int eid:w2e[w]) for(auto [s,t,qid]:e2stq[eid]) if (!uf.same(s, t)) ans[qid] = 0;
  }

  uf = UnionFind(n);
  rep(w,0,m){
    // 离散化 构建小图
    vector<int> ls;
    for (int eid: w2e[w]) {
      ls.push_back(uf.leader(u[eid]));
      ls.push_back(uf.leader(v[eid]));
    }
    sort(ls.begin(), ls.end());
    ls.erase(unique(ls.begin(), ls.end()), ls.end());
    auto lb=[&](int u)->int{ return lower_bound(ls.begin(), ls.end(), uf.leader(u)) - ls.begin();};

    int sz = ls.size();
    vector<vector<pair<int, int>>> G(sz); // G[u] = array {v, edgeid}
    for (int eid: w2e[w]) if(u[eid] != v[eid]){
      int nu = lb(u[eid]);
      int nv = lb(v[eid]);
      G[nu].emplace_back(nv, eid);
      G[nv].emplace_back(nu, eid);
    }
    lowlink lk(G); // 每次构建 <= 2边, 总代价小于等于O(2m)
    for(auto eid:w2e[w]) for(auto [s,t,qid]:e2stq[eid]) if(ans[qid] == -1) {
      int ns = lb(s);
      int nt = lb(t);
      assert(ns != nt);
      assert(ns < sz and nt < sz);
      assert(ls[ns] == uf.leader(s) and ls[nt] == uf.leader(t));
      // 已经保证了 加了=w的 所有边 s,t会连通, 只需要查询eid能否让它们断开
      ans[qid] = lk.query(eid, ns, nt);
    }
    for (int eid: w2e[w]) uf.merge(u[eid], v[eid]);
  }
  rep(i,0,q) printf("%d\n", ans[i]);
  return 0;
}

提出情報

提出日時
問題 Ex - Difference of Distance
ユーザ cromarmot
言語 C++ (GCC 9.2.1)
得点 625
コード長 3598 Byte
結果 AC
実行時間 500 ms
メモリ 64076 KiB

コンパイルエラー

./Main.cpp: In function ‘ll read()’:
./Main.cpp:6:21: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
    6 | ll read(){ll r;scanf("%lld",&r);return r;}
      |                ~~~~~^~~~~~~~~~~

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 625 / 625
結果
AC × 2
AC × 65
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 01_random1_00.txt, 01_random1_01.txt, 01_random1_02.txt, 01_random1_03.txt, 01_random1_04.txt, 01_random1_05.txt, 01_random1_06.txt, 01_random1_07.txt, 01_random1_08.txt, 01_random1_09.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 02_random2_05.txt, 02_random2_06.txt, 02_random2_07.txt, 02_random2_08.txt, 02_random2_09.txt, 02_random2_10.txt, 02_random2_11.txt, 02_random2_12.txt, 02_random2_13.txt, 02_random2_14.txt, 02_random2_15.txt, 02_random2_16.txt, 02_random2_17.txt, 02_random2_18.txt, 02_random2_19.txt, 02_random2_20.txt, 03_random3_00.txt, 03_random3_01.txt, 03_random3_02.txt, 03_random3_03.txt, 03_random3_04.txt, 03_random3_05.txt, 03_random3_06.txt, 03_random3_07.txt, 03_random3_08.txt, 03_random3_09.txt, 03_random3_10.txt, 03_random3_11.txt, 03_random3_12.txt, 03_random3_13.txt, 03_random3_14.txt, 03_random3_15.txt, 04_random4_00.txt, 04_random4_01.txt, 04_random4_02.txt, 04_random4_03.txt, 04_random4_04.txt, 05_random5_00.txt, 05_random5_01.txt, 05_random5_02.txt, 05_random5_03.txt, 05_random5_04.txt, 05_random5_05.txt, 05_random5_06.txt, 05_random5_07.txt, 05_random5_08.txt, 05_random5_09.txt, 06_min_00.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 6 ms 3600 KiB
00_sample_01.txt AC 2 ms 3704 KiB
01_random1_00.txt AC 287 ms 23708 KiB
01_random1_01.txt AC 300 ms 23724 KiB
01_random1_02.txt AC 308 ms 23728 KiB
01_random1_03.txt AC 319 ms 23988 KiB
01_random1_04.txt AC 340 ms 24108 KiB
01_random1_05.txt AC 340 ms 24364 KiB
01_random1_06.txt AC 338 ms 24656 KiB
01_random1_07.txt AC 345 ms 24512 KiB
01_random1_08.txt AC 347 ms 24784 KiB
01_random1_09.txt AC 369 ms 24656 KiB
02_random2_00.txt AC 500 ms 62452 KiB
02_random2_01.txt AC 410 ms 56436 KiB
02_random2_02.txt AC 406 ms 55788 KiB
02_random2_03.txt AC 408 ms 52052 KiB
02_random2_04.txt AC 407 ms 47112 KiB
02_random2_05.txt AC 391 ms 45976 KiB
02_random2_06.txt AC 406 ms 43644 KiB
02_random2_07.txt AC 438 ms 64076 KiB
02_random2_08.txt AC 423 ms 59752 KiB
02_random2_09.txt AC 414 ms 53924 KiB
02_random2_10.txt AC 424 ms 50120 KiB
02_random2_11.txt AC 415 ms 45848 KiB
02_random2_12.txt AC 397 ms 45856 KiB
02_random2_13.txt AC 405 ms 43576 KiB
02_random2_14.txt AC 428 ms 59544 KiB
02_random2_15.txt AC 393 ms 53152 KiB
02_random2_16.txt AC 411 ms 56400 KiB
02_random2_17.txt AC 407 ms 53004 KiB
02_random2_18.txt AC 414 ms 50004 KiB
02_random2_19.txt AC 407 ms 47736 KiB
02_random2_20.txt AC 424 ms 46408 KiB
03_random3_00.txt AC 336 ms 38148 KiB
03_random3_01.txt AC 313 ms 36856 KiB
03_random3_02.txt AC 326 ms 34572 KiB
03_random3_03.txt AC 301 ms 33816 KiB
03_random3_04.txt AC 336 ms 37980 KiB
03_random3_05.txt AC 316 ms 37204 KiB
03_random3_06.txt AC 327 ms 33936 KiB
03_random3_07.txt AC 296 ms 33876 KiB
03_random3_08.txt AC 327 ms 39124 KiB
03_random3_09.txt AC 268 ms 34180 KiB
03_random3_10.txt AC 294 ms 34056 KiB
03_random3_11.txt AC 297 ms 34768 KiB
03_random3_12.txt AC 301 ms 35556 KiB
03_random3_13.txt AC 282 ms 34756 KiB
03_random3_14.txt AC 253 ms 32820 KiB
03_random3_15.txt AC 288 ms 34268 KiB
04_random4_00.txt AC 346 ms 24888 KiB
04_random4_01.txt AC 337 ms 24660 KiB
04_random4_02.txt AC 335 ms 24800 KiB
04_random4_03.txt AC 385 ms 27160 KiB
04_random4_04.txt AC 372 ms 27272 KiB
05_random5_00.txt AC 294 ms 23460 KiB
05_random5_01.txt AC 306 ms 23304 KiB
05_random5_02.txt AC 288 ms 23264 KiB
05_random5_03.txt AC 297 ms 23540 KiB
05_random5_04.txt AC 295 ms 23692 KiB
05_random5_05.txt AC 297 ms 23440 KiB
05_random5_06.txt AC 304 ms 23580 KiB
05_random5_07.txt AC 304 ms 23580 KiB
05_random5_08.txt AC 303 ms 23576 KiB
05_random5_09.txt AC 299 ms 23340 KiB
06_min_00.txt AC 6 ms 3608 KiB