提出 #42099268


ソースコード 拡げる

#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:
    unordered_map<int,int> f;
    unordered_map<int,int> cnt;
    UnionFind(int sz){ // 0-index
      f=unordered_map<int,int>();
      rep(i,0,sz) f[i]=i;
      rep(i,0,sz) cnt[i]=1;
    }
    UnionFind(const unordered_map<int,int>&part){ // 0-index, 部分拷贝, 只留根
      f=part;
      cnt = unordered_map<int,int>();
      for(auto [k,_]:f) cnt[k] = 1;
    }
    int root(int u){ return f[u]==u?u:f[u]=root(f[u]); }
    void link(int u,int v){
      int fu=root(u);
      int fv=root(v);
      if(fu == fv) return ;
      if(cnt[fu] > cnt[fv]){
        f[fv] = fu;
        cnt[fu] += cnt[fv];
      }else{
        f[fu] = fv;
        cnt[fv] += cnt[fu];
      }
    }
};

vector<array<int,2> > edge; // u,v
vector<array<int,3> > eid2q[200010]; // s,t,qid
vector<int> ans;

void addEdge(const vector<int>&eids,int l,int r,UnionFind&uf){
  rep(i,l,r) {
    auto [u,v] = edge[eids[i]];
    uf.link(u,v);
  }
}

// [l,r)
void solve(const vector<int>&eids,int l,int r,const UnionFind&_uf){
  int sz = r-l;
  if(sz == 1){
    auto uf=_uf;
    int eid=eids[l];
    auto [u,v] = edge[eid];
    int fu=uf.root(u);
    int fv=uf.root(v);
    if(fu==fv) return ;
    for(auto [s,t,qid]:eid2q[eid]) {
      int fs = uf.root(s);
      int ft = uf.root(t);
      if(fs != ft and (
           (fs == fu and ft == fv) or
           (fs == fv and ft == fu)
           )){
        ans[qid] = true;
      }
    }
    return ;
  }
  auto ufr = _uf;
  addEdge(eids,l     ,l+sz/2,ufr);
  solve  (eids,l+sz/2,r     ,ufr);

  auto ufl = _uf;
  addEdge(eids,l+sz/2,r     ,ufl);
  solve  (eids,l     ,l+sz/2,ufl);
}

int main(){
  int n=read();
  int m=read();
  vector< vector<int> > w2eid(m+1); // weight 2 eid
  rep(i,0,m){
    int u=read()-1;
    int v=read()-1;
    int w=read();
    w2eid[w].push_back(i);
    edge.push_back({u,v});
  }
  int q=read();
  rep(i,0,q){
    int eid=read()-1;
    int s=read()-1;
    int t=read()-1;
    eid2q[eid].push_back({s,t,i});
  }
  ans = vector<int>(q);
  UnionFind uf(n);
  rep(w,1,m+1) if(w2eid[w].size()) {
    unordered_map<int,int> sub; // 部分拷贝的并查集
    auto add=[&](int u){ if(!sub.count(u)) { // 只加根
      assert(u == uf.f[u]);
      sub[u] = uf.f[u];
    } };

    for(auto eid:w2eid[w]){
      auto &[u,v]=edge[eid];
      u = uf.root(u);
      add(u);
      v = uf.root(v);
      add(v);
    }
    for(auto eid:w2eid[w]){
      per(i,0,size(eid2q[eid])) {
        auto &[s,t,qid] = eid2q[eid][i];
        s = uf.root(s);
        t = uf.root(t);
        if(!sub.count(s) or !sub.count(t)){ // 有不涉及的点,那么连完依然不涉及, 直接忽略
          swap(eid2q[eid][i],eid2q[eid].back());
          eid2q[eid].pop_back();
        }
      }
    }
    solve(w2eid[w],0,size(w2eid[w]), UnionFind(sub));
    addEdge(w2eid[w],0,size(w2eid[w]), uf);
  }
  for(auto o:ans) printf("%d\n",o);
  return 0;
}

提出情報

提出日時
問題 Ex - Difference of Distance
ユーザ cromarmot
言語 C++ (GCC 9.2.1)
得点 0
コード長 3108 Byte
結果 TLE
実行時間 5527 ms
メモリ 457604 KiB

コンパイルエラー

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

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 0 / 625
結果
AC × 2
AC × 28
TLE × 37
セット名 テストケース
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 13 ms 8440 KiB
00_sample_01.txt AC 7 ms 8440 KiB
01_random1_00.txt AC 399 ms 25240 KiB
01_random1_01.txt AC 424 ms 26632 KiB
01_random1_02.txt AC 467 ms 28152 KiB
01_random1_03.txt AC 494 ms 30524 KiB
01_random1_04.txt AC 514 ms 31476 KiB
01_random1_05.txt AC 546 ms 32728 KiB
01_random1_06.txt AC 575 ms 36740 KiB
01_random1_07.txt AC 612 ms 37408 KiB
01_random1_08.txt AC 644 ms 38636 KiB
01_random1_09.txt AC 735 ms 39928 KiB
02_random2_00.txt TLE 5526 ms 457344 KiB
02_random2_01.txt TLE 5526 ms 455756 KiB
02_random2_02.txt TLE 5526 ms 455988 KiB
02_random2_03.txt TLE 5526 ms 456084 KiB
02_random2_04.txt TLE 5526 ms 455976 KiB
02_random2_05.txt TLE 5526 ms 455072 KiB
02_random2_06.txt TLE 5525 ms 374004 KiB
02_random2_07.txt TLE 5527 ms 457344 KiB
02_random2_08.txt TLE 5527 ms 455724 KiB
02_random2_09.txt TLE 5527 ms 455972 KiB
02_random2_10.txt TLE 5527 ms 456128 KiB
02_random2_11.txt TLE 5527 ms 455692 KiB
02_random2_12.txt TLE 5527 ms 455064 KiB
02_random2_13.txt TLE 5525 ms 387388 KiB
02_random2_14.txt TLE 5527 ms 457604 KiB
02_random2_15.txt TLE 5527 ms 455556 KiB
02_random2_16.txt TLE 5527 ms 456068 KiB
02_random2_17.txt TLE 5527 ms 456020 KiB
02_random2_18.txt TLE 5527 ms 455832 KiB
02_random2_19.txt TLE 5527 ms 455076 KiB
02_random2_20.txt TLE 5525 ms 387240 KiB
03_random3_00.txt TLE 5521 ms 238584 KiB
03_random3_01.txt TLE 5521 ms 238524 KiB
03_random3_02.txt TLE 5521 ms 238340 KiB
03_random3_03.txt TLE 5521 ms 237504 KiB
03_random3_04.txt TLE 5521 ms 238284 KiB
03_random3_05.txt TLE 5521 ms 238840 KiB
03_random3_06.txt TLE 5521 ms 237672 KiB
03_random3_07.txt TLE 5521 ms 237404 KiB
03_random3_08.txt TLE 5521 ms 238512 KiB
03_random3_09.txt TLE 5521 ms 237204 KiB
03_random3_10.txt TLE 5521 ms 238304 KiB
03_random3_11.txt TLE 5521 ms 238492 KiB
03_random3_12.txt TLE 5521 ms 238172 KiB
03_random3_13.txt TLE 5521 ms 237556 KiB
03_random3_14.txt TLE 5521 ms 236740 KiB
03_random3_15.txt TLE 5521 ms 238120 KiB
04_random4_00.txt AC 656 ms 39900 KiB
04_random4_01.txt AC 624 ms 39912 KiB
04_random4_02.txt AC 701 ms 40024 KiB
04_random4_03.txt AC 575 ms 42172 KiB
04_random4_04.txt AC 535 ms 42172 KiB
05_random5_00.txt AC 484 ms 30884 KiB
05_random5_01.txt AC 484 ms 30944 KiB
05_random5_02.txt AC 475 ms 30860 KiB
05_random5_03.txt AC 474 ms 30956 KiB
05_random5_04.txt AC 479 ms 30936 KiB
05_random5_05.txt AC 481 ms 30852 KiB
05_random5_06.txt AC 471 ms 30924 KiB
05_random5_07.txt AC 476 ms 30940 KiB
05_random5_08.txt AC 505 ms 30888 KiB
05_random5_09.txt AC 478 ms 30780 KiB
06_min_00.txt AC 9 ms 8400 KiB