Submission #11592997
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
template<typename T>
istream& operator >> (istream& istr, vector<T>& v){
for(T& x: v) istr >> x;
return istr;
}
template<typename T, typename U>
istream& operator >> (istream& istr, pair<T,U>& p){
istr >> p.first >> p.second;
return istr;
}
template<typename T>
ostream& operator << (ostream& ostr, vector<T>& v){
if(!v.empty()){
ostr << v.front();
for(auto itr = ++v.begin(); itr != v.end(); itr++)
// ostr << *itr;
ostr << " " << *itr;
}
return ostr;
}
template<typename T, typename U>
ostream& operator << (ostream& ostr, pair<T,U>& p){
// ostr << p.first << p.second;
ostr << p.first << ", " << p.second;
return ostr;
}
template<typename T>
ostream& operator << (ostream& ostr, vector<vector<T>>& vv){
if(!vv.empty()){
ostr << vv.front();
for(auto itr = ++vv.begin(); itr != vv.end(); itr++)
ostr << endl << *itr;
}
return ostr;
}
template<typename T, typename U>
ostream& operator << (ostream& ostr, vector<pair<T,U>>& vp){
if(!vp.empty()){
ostr << vp.front();
for(auto itr = ++vp.begin(); itr != vp.end(); itr++)
ostr << endl << *itr;
}
return ostr;
}
typedef long long ll;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<double> vd;
// vll v(n); v[i]=x;
typedef vector<vll> vvll;
typedef vector<vc> vvc;
typedef vector<vd> vvd;
// vvll v(m,vll(n)); v[i][j]=x;
typedef pair<ll,ll> pll;
typedef pair<double,double> pd;
typedef vector<pll> vpll;
typedef vector<pd> vpd;
// vpll v(n); v[i]={x,y}; v[i].first/second;
// v.front() : v[0]
// v.back() : v[v.size()-1]
#define PI 3.1415926535897932
#define EPS 1e-12
#define INF (ll)1e+12
#define REP(i,n) for(ll i=0; i<(n); i++)
#define RREP(i,n) for(ll i=(n)-1; i>=0; i--)
#define FOR(i,a,b) for(ll i=(a); i<=(b); i++)
#define FORR(i,a,b) for(ll i=(b); i>=(a); i--)
#define fix(x) cout << fixed << setprecision(x)
#define dump(x) cout << #x << " = " << (x) << endl
#define all(x) (x).begin(),(x).end()
template<typename T> T sum(vector<T>& v){return accumulate(all(v), (T)0);}
template<typename T> inline void sort(vector<T>& v){sort(all(v));}
template<typename T> inline void rsort(vector<T>& v){sort(all(v), greater<T>());}
template<typename T> inline void chmin(T& a, T b){if(a>b) a=b;}
template<typename T> inline void chmax(T& a, T b){if(a<b) a=b;}
// 重み付きUnion-Find tree
class UnionFind{
public:
vll par;
vll rank;
vll diffw;
UnionFind(ll n);
ll root(ll x);
ll weight(ll x);
ll diff(ll x, ll y);
bool issame(ll x, ll y);
bool merge(ll x, ll y, ll w);
};
UnionFind::UnionFind(ll n){
par.resize(n);
rank.resize(n);
diffw.resize(n);
REP(i,n) par[i]=i, rank[i]=0, diffw[i]=0;
}
ll UnionFind::root(ll x){
if (par[x] == x) return x;
else{
ll r = root(par[x]);
diffw[x] += diffw[par[x]];
return par[x] = r;
}
}
ll UnionFind::weight(ll x){
root(x);
return diffw[x];
}
ll UnionFind::diff(ll x, ll y){
return weight(y) - weight(x);
}
bool UnionFind::issame(ll x, ll y){
return root(x) == root(y);
}
bool UnionFind::merge(ll x, ll y, ll w=0){
w = w + weight(x)- weight(y);
x = root(x);
y = root(y);
if(x==y) return false;
if(rank[x] < rank[y]) swap(x, y), w = -w;
if(rank[x] == rank[y]) rank[x]++;
par[y] = x;
diffw[y] = w;
return true;
}
int main(){
ll n, q;
cin >> n >> q;
vll p(q), a(q), b(q);
REP(i,q) cin >> p[i] >> a[i] >> b[i];
UnionFind uf(n);
REP(i,q){
if(p[i]){
if(uf.issame(a[i], b[i])) cout << "Yes" << endl;
else cout << "No" << endl;
}
else uf.merge(a[i], b[i]);
}
return 0;
}
Submission Info
Submission Time |
|
Task |
B - Union Find |
User |
sin_1 |
Language |
C++14 (GCC 5.4.1) |
Score |
100 |
Code Size |
3971 Byte |
Status |
AC |
Exec Time |
463 ms |
Memory |
7936 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
100 / 100 |
Status |
|
|
Set Name |
Test Cases |
Sample |
00_sample_01.txt |
All |
00_sample_01.txt, subtask_01_01.txt, subtask_01_02.txt, subtask_01_03.txt, subtask_01_04.txt, subtask_01_05.txt, subtask_01_06.txt, subtask_01_07.txt, subtask_01_08.txt, subtask_01_09.txt, subtask_01_10.txt, subtask_01_11.txt, subtask_01_12.txt, subtask_01_13.txt, subtask_01_14.txt, subtask_01_15.txt, subtask_01_16.txt, subtask_01_17.txt, subtask_01_18.txt |
Case Name |
Status |
Exec Time |
Memory |
00_sample_01.txt |
AC |
1 ms |
256 KB |
subtask_01_01.txt |
AC |
258 ms |
3712 KB |
subtask_01_02.txt |
AC |
2 ms |
2560 KB |
subtask_01_03.txt |
AC |
392 ms |
5760 KB |
subtask_01_04.txt |
AC |
463 ms |
7808 KB |
subtask_01_05.txt |
AC |
24 ms |
640 KB |
subtask_01_06.txt |
AC |
26 ms |
2944 KB |
subtask_01_07.txt |
AC |
406 ms |
5504 KB |
subtask_01_08.txt |
AC |
442 ms |
7936 KB |
subtask_01_09.txt |
AC |
1 ms |
256 KB |
subtask_01_10.txt |
AC |
2 ms |
2560 KB |
subtask_01_11.txt |
AC |
394 ms |
5504 KB |
subtask_01_12.txt |
AC |
438 ms |
7936 KB |
subtask_01_13.txt |
AC |
337 ms |
4480 KB |
subtask_01_14.txt |
AC |
3 ms |
2560 KB |
subtask_01_15.txt |
AC |
402 ms |
5504 KB |
subtask_01_16.txt |
AC |
449 ms |
7808 KB |
subtask_01_17.txt |
AC |
298 ms |
7552 KB |
subtask_01_18.txt |
AC |
297 ms |
7552 KB |