Exemplo n.º 1
0
int main () {
    scanf("%d", &t);

    flow.init();
    while (t--) {
        scanf("%d", &n);

        flow.src = 0;
        flow.snk = n-1;
        flow.n = n;

        for (int i = 0; i < n-1; i++) {
            int m, a;
            scanf("%d", &m);

            for (int j = 0; j < m; j++) {
                scanf("%d", &a); a--;
                if (!i || a == n-1)
                    flow.connect(i, a, 1);
                else
                    flow.connect(i, a, LLONG_MAX);
            }

        }

        printf("%lld\n", flow.maxflow());
        if (t) {
            flow.reset();
        }
    }
}
Exemplo n.º 2
0
int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		int n, m;
		scanf("%d %d", &n, &m);
		int a, b, mid;
		scanf("%d %d %d", &a, &b, &mid);
		din.edges.clear();
		for (int i = 0; i < maxn; ++i)
			din.G[i].clear();
		for (int i = 0; i < m; ++i)
		{
			int x, y;
			scanf("%d %d", &x, &y);
			din.addedge(x * 2 + 1, y * 2, 1);
			din.addedge(y * 2 + 1, x * 2, 1);
		}
		for (int i = 1; i <= n; ++i)
			din.addedge(i * 2, i * 2 + 1, i == mid ? 2 : 1);
		int s = 0;
		int t = 1;
		din.addedge(s, mid * 2, 2);
		din.addedge(a * 2 + 1, n * 2 + 2, 1);
		din.addedge(b * 2 + 1, n * 2 + 2, 1);
		din.addedge(n * 2 + 2, t, 2);
		din.maxflow(s, t);
		for (int i = 0; i < m; ++i)
		{
			if (din.edges[i * 4].flow)
				pre[din.edges[i * 4].to / 2] = din.edges[i * 4].from / 2;
			if (din.edges[i * 4 + 2].flow)
				pre[din.edges[i * 4 + 2].to / 2] = din.edges[i * 4 + 2].from / 2;
		}
		vector<int> ans1, ans2;
		while (a != mid)
		{
			ans1.push_back(a);
			a = pre[a];
		}
		while (b != mid)
		{
			ans2.push_back(b);
			b = pre[b];
		}
		for (int i = 0; i < ans1.size(); ++i)
			printf("%d ", ans1[i]);
		printf("%d", mid);
		for (int i = (int)ans2.size() - 1; i >= 0; --i)
			printf(" %d", ans2[i]);
		printf("\n");
	}
	return 0;
}