結果
問題 | No.416 旅行会社 |
ユーザー | pione |
提出日時 | 2020-04-30 04:39:48 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 430 ms / 4,000 ms |
コード長 | 3,230 bytes |
コンパイル時間 | 1,795 ms |
コンパイル使用メモリ | 182,944 KB |
実行使用メモリ | 29,696 KB |
最終ジャッジ日時 | 2024-05-08 15:53:59 |
合計ジャッジ時間 | 7,370 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 218 ms
19,712 KB |
testcase_01 | AC | 3 ms
5,760 KB |
testcase_02 | AC | 4 ms
5,888 KB |
testcase_03 | AC | 2 ms
5,760 KB |
testcase_04 | AC | 3 ms
6,016 KB |
testcase_05 | AC | 3 ms
5,760 KB |
testcase_06 | AC | 3 ms
5,760 KB |
testcase_07 | AC | 5 ms
5,888 KB |
testcase_08 | AC | 8 ms
6,016 KB |
testcase_09 | AC | 27 ms
7,168 KB |
testcase_10 | AC | 253 ms
19,712 KB |
testcase_11 | AC | 255 ms
25,856 KB |
testcase_12 | AC | 261 ms
26,112 KB |
testcase_13 | AC | 227 ms
25,984 KB |
testcase_14 | AC | 420 ms
29,696 KB |
testcase_15 | AC | 428 ms
29,676 KB |
testcase_16 | AC | 418 ms
29,584 KB |
testcase_17 | AC | 426 ms
29,696 KB |
testcase_18 | AC | 430 ms
29,568 KB |
testcase_19 | AC | 308 ms
20,096 KB |
testcase_20 | AC | 318 ms
20,096 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; } }