結果
問題 | No.1293 2種類の道路 |
ユーザー |
![]() |
提出日時 | 2020-11-21 16:02:33 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 127 ms / 2,000 ms |
コード長 | 1,741 bytes |
コンパイル時間 | 1,661 ms |
コンパイル使用メモリ | 178,180 KB |
実行使用メモリ | 13,492 KB |
最終ジャッジ日時 | 2024-07-23 15:43:22 |
合計ジャッジ時間 | 4,213 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 22 |
ソースコード
#include<bits/stdc++.h>using namespace std;//#define int long long#define REP(i,m,n) for(int i=(m);i<(n);i++)#define rep(i,n) REP(i,0,n)#define pb push_back#define all(a) a.begin(),a.end()#define rall(c) (c).rbegin(),(c).rend()#define mp make_pair#define endl '\n'#define vec vector<ll>#define mat vector<vector<ll> >#define fi first#define se secondtypedef long long ll;typedef unsigned long long ull;typedef pair<ll,ll> pll;typedef long double ld;typedef complex<double> comp;const ll INF=1e9+7;const ll inf=INF*INF;const ll MOD=1e9+7;const ll mod=MOD;const ll MAX=200010;const double PI=acos(-1.0);//Union-Find木struct UnionFind {vector< int > data;UnionFind() = default;explicit UnionFind(size_t sz) : data(sz, -1) {}bool unite(int x, int y) {x = find(x), y = find(y);if(x == y) return false;if(data[x] > data[y]) swap(x, y);data[x] += data[y];data[y] = x;return true;}int find(int k) {if(data[k] < 0) return (k);return data[k] = find(data[k]);}int size(int k) {return -data[find(k)];}bool same(int x, int y) {return find(x) == find(y);}};signed main(){ll n,d,w;cin>>n>>d>>w;UnionFind uf(n);ll ans=0;rep(i,d){ll a,b;cin>>a>>b;a--;b--;uf.unite(a,b);}UnionFind uf2(n);rep(i,w){ll a,b;cin>>a>>b;a--;b--;uf2.unite(a,b);}vector<set<ll> >st(n);rep(i,n){ll rd=uf.find(i);ll rw=uf2.find(i);if(!st[rd].count(rw)){st[rd].insert(rw);ans+=1LL*uf.size(rd)*uf2.size(rw);}}cout<<ans-n<<endl;}