

赛场上以为是并查集,结果并了半天查不出来,淦。。。
后来才知道是一个简单的深搜,首先找到最长的串,如果长度相同,看看谁小就存谁,vector可以直接进行比较。
#include<iostream>
#include<stack>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
map<int, int>mp;
const int N = 50010;
int e[N], ne[N], h[N], idx;
int res;
bool st[N];
vector<int>ans, now;
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void dfs(int x, int n) {
if (n > res) {
res = n;
ans = now;
}
else if (n == res && now < ans)ans = now;
for (int i = h[x]; i != -1; i = ne[i]) {
int j = e[i];
now.push_back(j);
dfs(j, n + 1);
now.pop_back();
}
}
int main()
{
int n;
cin >> n;
memset(h, -1, sizeof h);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
for (int j = 1; j <= x; j++) {
int a;
cin >> a;
add(i, a);
st[a] = true;
}
}
int xx = 0;
while (st[xx])xx++;
dfs(xx, 1);
cout << res << endl << xx ;
for (auto p : ans) {
cout << ' ' << p;
}
}
3848

被折叠的 条评论
为什么被折叠?



