結果
問題 | No.416 旅行会社 |
ユーザー | pione |
提出日時 | 2020-04-30 04:39:48 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 414 ms / 4,000 ms |
コード長 | 3,230 bytes |
コンパイル時間 | 1,951 ms |
コンパイル使用メモリ | 182,944 KB |
実行使用メモリ | 29,664 KB |
最終ジャッジ日時 | 2024-12-14 20:52:03 |
合計ジャッジ時間 | 7,312 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 217 ms
19,712 KB |
testcase_01 | AC | 4 ms
5,888 KB |
testcase_02 | AC | 3 ms
5,760 KB |
testcase_03 | AC | 4 ms
5,760 KB |
testcase_04 | AC | 4 ms
5,760 KB |
testcase_05 | AC | 4 ms
6,016 KB |
testcase_06 | AC | 4 ms
5,760 KB |
testcase_07 | AC | 5 ms
5,760 KB |
testcase_08 | AC | 8 ms
6,016 KB |
testcase_09 | AC | 27 ms
6,912 KB |
testcase_10 | AC | 240 ms
19,656 KB |
testcase_11 | AC | 245 ms
25,856 KB |
testcase_12 | AC | 239 ms
25,924 KB |
testcase_13 | AC | 212 ms
25,984 KB |
testcase_14 | AC | 408 ms
29,664 KB |
testcase_15 | AC | 414 ms
29,664 KB |
testcase_16 | AC | 406 ms
29,440 KB |
testcase_17 | AC | 393 ms
29,484 KB |
testcase_18 | AC | 402 ms
29,456 KB |
testcase_19 | AC | 306 ms
20,164 KB |
testcase_20 | AC | 307 ms
20,036 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[100100]; bool visited[100100]; ll res[100100]; void dfs(ll v, ll p, ll c){ res[v]=c; visited[v]=true; for(auto nv: G[v]){ if(nv==p) continue; if(visited[nv]) continue; dfs(nv,v,c); } } signed main() { cin.tie( 0 ); ios::sync_with_stdio( false ); cin>>n>>m>>q; vpll ab(m); rep(i,m){ ll a,b; cin>>a>>b; a--,b--; ab[i]={a,b}; } vpll cd(q); set<pll> s; rep(i,q){ ll c,d; cin>>c>>d; c--,d--; cd[i]={c,d}; s.insert({c,d}); } UnionFind uf(n); rep(i,m){ if(!s.count(ab[i])){ ll a,b; tie(a,b)=ab[i]; G[a].push_back(b); G[b].push_back(a); uf.connect(a,b); } } rep(i,n){ if(uf.isSame(0,i)){ visited[i]=true; res[i]=-1; } } // rep(i,n){ // if(i) cout<<' '; // cout<<res[i]; // } // cout<<endl; rrep(i,q){ ll c,d; tie(c,d)=cd[i]; if(!uf.isSame(c,d)){ G[c].push_back(d); G[d].push_back(c); if(uf.isSame(0,c)) dfs(d,-1,i+1); if(uf.isSame(0,d)) dfs(c,-1,i+1); uf.connect(c,d); } } irep(i,1,n){ cout<<res[i]<<endl; } }