L2 - 2 病毒溯源

在这里插入图片描述
在这里插入图片描述
赛场上以为是并查集,结果并了半天查不出来,淦。。。
后来才知道是一个简单的深搜,首先找到最长的串,如果长度相同,看看谁小就存谁,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;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值