結果
問題 | No.416 旅行会社 |
ユーザー | pione |
提出日時 | 2020-05-01 03:20:14 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 456 ms / 4,000 ms |
コード長 | 3,096 bytes |
コンパイル時間 | 1,859 ms |
コンパイル使用メモリ | 182,972 KB |
実行使用メモリ | 26,496 KB |
最終ジャッジ日時 | 2024-12-14 20:51:46 |
合計ジャッジ時間 | 7,478 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 206 ms
16,640 KB |
testcase_01 | AC | 5 ms
8,192 KB |
testcase_02 | AC | 4 ms
8,064 KB |
testcase_03 | AC | 5 ms
8,192 KB |
testcase_04 | AC | 4 ms
8,192 KB |
testcase_05 | AC | 5 ms
8,192 KB |
testcase_06 | AC | 5 ms
8,064 KB |
testcase_07 | AC | 5 ms
8,108 KB |
testcase_08 | AC | 9 ms
8,320 KB |
testcase_09 | AC | 29 ms
9,088 KB |
testcase_10 | AC | 214 ms
16,512 KB |
testcase_11 | AC | 211 ms
22,912 KB |
testcase_12 | AC | 226 ms
22,912 KB |
testcase_13 | AC | 211 ms
22,708 KB |
testcase_14 | AC | 442 ms
24,448 KB |
testcase_15 | AC | 440 ms
25,472 KB |
testcase_16 | AC | 438 ms
26,496 KB |
testcase_17 | AC | 453 ms
25,216 KB |
testcase_18 | AC | 456 ms
24,832 KB |
testcase_19 | AC | 326 ms
21,248 KB |
testcase_20 | AC | 330 ms
20,480 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; // #define int long long #define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i) #define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i) #define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--) #define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--) #define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i) #define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i) #define SORT(v, n) sort(v, v + n); #define REVERSE(v, n) reverse(v, v+n); #define vsort(v) sort(v.begin(), v.end()); #define all(v) v.begin(), v.end() #define mp(n, m) make_pair(n, m); #define cout(d) cout<<d<<endl; #define coutd(d) cout<<std::setprecision(10)<<d<<endl; #define cinline(n) getline(cin,n); #define replace_all(s, b, a) replace(s.begin(),s.end(), b, a); #define PI (acos(-1)) #define FILL(v, n, x) fill(v, v + n, x); #define sz(x) long long(x.size()) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using pii = pair<int, int>; using pll = pair<ll, ll>; using vs = vector<string>; using vpll = vector<pair<ll, ll>>; using vtp = vector<tuple<ll,ll,ll>>; using vb = vector<bool>; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const ll INF = 1e9; const ll MOD = 1e9+7; const ll LINF = 1e18; class UnionFind{ private: vector<ll> Parent; public: UnionFind(ll N){ Parent = vector<ll>(N, -1); } ll root(ll A){ if(Parent[A] < 0) return A; return Parent[A] = root(Parent[A]); } ll size(ll A){ return -Parent[root(A)]; } bool connect(ll A, ll B){ A = root(A); B = root(B); if(A == B){ return false; } if(size(A) < size(B)) swap(A, B); Parent[A] += Parent[B]; Parent[B] = A; return true; } bool isSame(ll A, ll B){ return root(A) == root(B); } }; ll n,m,q; vll G[200100]; ll res[200100]; bool visited[200100]; void dfs(ll v, ll p, ll cnt){ res[v]=cnt; for(auto nv: G[v]){ if(nv==p) continue; if(visited[nv]) continue; visited[nv]=true; dfs(nv,v,cnt); } } signed main() { cin.tie( 0 ); ios::sync_with_stdio( false ); cin>>n>>m>>q; set<pll> s; rep(i,m){ ll a,b; cin>>a>>b; a--,b--; s.insert({a,b}); } vpll cd(q); rep(i,q){ ll c,d; cin>>c>>d; c--,d--; cd[i]={c,d}; s.erase(cd[i]); } UnionFind uf(n); for(auto e: s){ ll a,b; tie(a,b)=e; uf.connect(a,b); G[a].push_back(b); G[b].push_back(a); } rep(i,n){ if(uf.isSame(0,i)){ res[i]=-1; visited[i]=true; } } rrep(i,q){ ll c,d; tie(c,d)=cd[i]; if(!uf.isSame(c,d)){ if(uf.isSame(0,c)) dfs(d,-1,i+1); if(uf.isSame(0,d)) dfs(c,-1,i+1); uf.connect(c,d); } G[c].push_back(d); G[d].push_back(c); } irep(i,1,n){ cout<<res[i]<<endl; } }