出错了,数据一大就过不去,是Wrong错误,还不是TLE,求大佬给个指点
C++ 代码
#include <iostream>
#include <algorithm>
#include <unordered_set>
using namespace std;
const int N = 100004;
const int K = 13;
unordered_set<int> s;
int p[N];
void init(int n)
{
for(int i = 1; i <= n; i++)
{
p[i] = i;
}
}
int find(int x)
{
if(p[x] != x)
p[x] = p[p[x]];
return p[x];
}
void Union(int x, int y)
{
int x_root = find(x);
int y_root = find(y);
if(x_root != y_root)
{
p[x_root] = y_root; // y的根做根
}
}
int main()
{
int n = 0;
cin >> n;
int maxn = 0;
int a[N][K];
for(int i = 1; i <= n; i++)
{
int k = 0;
cin >> k;
for(int j = 1; j <= k; j++)
{
cin >> a[i][j];
s.insert(a[i][j]);
maxn = max(maxn,a[i][j]);
}
}
init(maxn);
for(int i = 1; i <= n; i++)
{
for(int j = 2; j <= 10 && a[i][j] != 0; j++)
{
if(find(a[i][j]) != find(a[i][j-1]))
{
Union(a[i][j],a[i][j-1]);
}
}
}
int cnt;
for(int i = 1; i <= maxn; i++)
{
if(p[i] == i && s.count(i))
cnt++;
}
cout << cnt << " ";
cout << s.size() << endl;
int q = 0;
cin >> q;
while(q--)
{
int a = 0;
int b = 0;
cin >> a >> b;
if(find(a) != find(b))
cout << "No" <<endl;
else
cout << "Yes" <<endl;
}
}
看我的吧,我也是数据大了就tle,是因为没考虑一只鸟单独在一颗树上的情况
我是数据大了TLE…