Submission #420234


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
#define mem(a) memset(a,0,sizeof(a))
#define pb(a) push_back(a)
#define in(a) insert(a)
#define R cin>>
#define F first
#define S second
#define C class
#define ll long long
#define ln cout<<'\n'
#define _(_1,_2,_3,N,...)N
#define pr(...) _(__VA_ARGS__,pr3,pr2,pr1)(__VA_ARGS__)
template<C T>void pr1(T a){cout<<a;ln;}
template<C T,C T2>void pr2(T a,T2 b){cout<<a<<' '<<b;ln;}
template<C T,C T2,C T3>void pr3(T a,T2 b,T3 c){cout<<a<<' '<<b<<' '<<c;ln;}
template<C T>void PR(T a,int n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
bool check(int n,int m,int x,int y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;

int p[100001],r[100001];
void init() {rep(i,100001) p[i]=i,r[i]=0;}
int find(int x) {return (p[x]==x)?x:(p[x]=find(p[x]));}
void unite(int x,int y) {
  x=find(x),y=find(y);
  if(x==y) return;
  if(r[x]<r[y]) p[x]=y;
  else {p[y]=x;if(r[x]==r[y]) r[x]++;}
}
bool same(int x, int y) {return find(x)==find(y);}

void Main() {
  init();
  int n,m;
  cin >> n >> m;
  rep(i,m) {
    int x,y,z;
    cin >> z >> x >> y;
    if(z) {
      if(same(x,y)) pr("Yes");
      else pr("No");
    } else unite(x,y);
  }
}

int main() {
  ios::sync_with_stdio(0);cin.tie(0);Main();
  return 0;
}

Submission Info

Submission Time
Task B - Union Find
User kzyKT
Language C++ (GCC 4.9.2)
Score 100
Code Size 1641 Byte
Status AC
Exec Time 138 ms
Memory 1700 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 1
AC × 19
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 26 ms 1560 KB
subtask_01_01.txt AC 88 ms 1568 KB
subtask_01_02.txt AC 26 ms 1700 KB
subtask_01_03.txt AC 138 ms 1576 KB
subtask_01_04.txt AC 127 ms 1564 KB
subtask_01_05.txt AC 32 ms 1692 KB
subtask_01_06.txt AC 33 ms 1640 KB
subtask_01_07.txt AC 137 ms 1568 KB
subtask_01_08.txt AC 134 ms 1564 KB
subtask_01_09.txt AC 26 ms 1568 KB
subtask_01_10.txt AC 26 ms 1568 KB
subtask_01_11.txt AC 132 ms 1508 KB
subtask_01_12.txt AC 133 ms 1568 KB
subtask_01_13.txt AC 111 ms 1692 KB
subtask_01_14.txt AC 27 ms 1572 KB
subtask_01_15.txt AC 128 ms 1496 KB
subtask_01_16.txt AC 129 ms 1564 KB
subtask_01_17.txt AC 112 ms 1572 KB
subtask_01_18.txt AC 125 ms 1508 KB