QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#184609 | #5672. Connectivity Problem | SayedHassan# | ML | 0ms | 0kb | C++14 | 637b | 2023-09-20 22:35:25 | 2023-09-20 22:35:26 |
answer
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1005;
int p[N],sz[N];
int find(int u)
{
if(p[u]!=u)return p[u]=find(p[u]);
}
bool merge(int u,int v)
{
u=find(u),v=find(v);
if(u==v)return true;
if(sz[u]<sz[v])swap(u,v);
p[v]=u;
sz[u]+=sz[v];
return false;
}
int main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
fill(sz,sz+N,1);
iota(p,p+N,0);
int n;
cin>>n;
for(int i=0;i<n;i++)
{
int u,v;
cin>>u>>v;
cout<<(merge(u,v)?"Y":"N")<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Memory Limit Exceeded
input:
12 3 4 4 9 8 1 2 3 5 6 2 9 5 9 7 3 4 8 5 6 1 8 6 1