Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- using namespace std;
- #define si(n) scanf("%d",&n)
- #define MAX 10004
- int parent[MAX],rnk[MAX];
- void make_set (int v) {
- parent[v] = v;
- rnk[v] = 0;
- }
- int find_set (int v) {
- if (v == parent[v])
- return v;
- return parent[v] = find_set (parent[v]);
- }
- void union_sets (int a, int b) {
- a = find_set (a);
- b = find_set (b);
- if (a != b) {
- if (rnk[a] < rnk[b])
- swap (a, b);
- parent[b] = a;
- if (rnk[a] == rnk[b])
- ++rnk[a];
- }
- }
- int main(){
- int n,m,i,j;
- //freopen("input.txt","r",stdin);
- si(n);si(m);
- for(i=1;i<=n;i++)
- make_set(i);
- for(i=1;i<=m;i++){
- int u,v;
- si(u);si(v);
- union_sets(u,v);
- }
- for(i=1;i<=10;i++){
- int u,v;
- si(u);si(v);
- if(find_set(u)!=find_set(v))
- cout<<"Not friend"<<endl;
- else cout<<"Friend"<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement