POJ 1904 King's Quest(强连通分量)

题目链接

Description

Once upon a time there lived a king and he had N sons. And there were N beautiful girls in the kingdom and the king knew about each of his sons which of those girls he did like. The sons of the king were young and light-headed, so it was possible for one son to like several girls.

So the king asked his wizard to find for each of his sons the girl he liked, so that he could marry her. And the king’s wizard did it – for each son the girl that he could marry was chosen, so that he liked this girl and, of course, each beautiful girl had to marry only one of the king’s sons.

However, the king looked at the list and said: “I like the list you have made, but I am not completely satisfied. For each son I would like to know all the girls that he can marry. Of course, after he marries any of those girls, for each other son you must still be able to choose the girl he likes to marry.”

The problem the king wanted the wizard to solve had become too hard for him. You must save wizard’s head by solving this problem.
Input

The first line of the input contains N – the number of king’s sons (1 <= N <= 2000). Next N lines for each of king’s sons contain the list of the girls he likes: first Ki – the number of those girls, and then Ki different integer numbers, ranging from 1 to N denoting the girls. The sum of all Ki does not exceed 200000.

The last line of the case contains the original list the wizard had made – N different integer numbers: for each son the number of the girl he would marry in compliance with this list. It is guaranteed that the list is correct, that is, each son likes the girl he must marry according to this list.

Output

Output N lines.For each king’s son first print Li – the number of different girls he likes and can marry so that after his marriage it is possible to marry each of the other king’s sons. After that print Li different integer numbers denoting those girls, in ascending order.
Sample Input

4
2 1 2
2 1 2
2 2 3
2 3 4
1 2 3 4

Sample Output

2 1 2
2 1 2
1 3
1 4

Hint

This problem has huge input and output data,use scanf() and printf() instead of cin and cout to read data to avoid time limit exceed.

题意

N个王子和N个公主,巫师已经为每个王子分配一个公主,但是皇帝想知道每个王子在保证每个王子都能够匹配的前提下能娶的公主有多少?

思路

  • 刚开始用二分图暴力删边 -> TLE
  • 强连通分量:王子向公主建边,公主向和她匹配的王子建边。这样保证每一对王子和公主都在一个强联通分量中,这是如果求得一个强联通分量,在分量中的王子和公主都可以相互结婚而之前已经匹配的也能找到新的关系,这样就符合题意

有两个坑点:

  • 输出公主编号要升序(in ascending order)
  • 同一个强连通分量其实也不能互相结婚,还有个前提是王子要喜欢才行,最后输出的时候要判断一下

scanf + printf + vector :12s
读写挂 + 链式向前星:500ms

#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cstring>
#include <cmath>
#include <algorithm>
#define lowbit(x) (x & (-x))
#define mem(a, b) memset(a, b, sizeof(a))
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define mid ((l + r)>>1)
#define lc rt<<1
#define rc rt<<1|1
typedef long long LL;
#define maxn 4004
using namespace std;
// __int128 read() {    __int128 x = 0, f = 1;  char c = getchar(); while (c < '0' || c > '9') {        if (c == '-') f = -1;       c = getchar();  }   while (c >= '0' && c <= '9') {      x = x * 10 + c - '0';       c = getchar();  }   return x * f;}
// void print(__int128 x) { if (x < 0) {        putchar('-');       x = -x; }   if (x > 9)  print(x / 10);  putchar(x % 10 + '0');}
int read() {
    int num = 0, flag = 1;
    char c = getchar();
    while (c < '0' || c > '9')  flag = (c == '-') ? -1 : 1, c = getchar();
    while (c >= '0' && c <= '9') num = (num << 3) + (num << 1) + c - '0', c = getchar();
    return num * flag;
}
void out(int x) {
    if (x > 9)  out(x / 10);
    putchar(x % 10 + '0');
}

vector<int> g[maxn];

int Stack[maxn], low[maxn], dfn[maxn], inStack[maxn], belong[maxn];
int now = 0, len = 0, cnt= 0, n;
int ans[maxn];

int head[300020], pre[300020], edge[300020], tot = 1;
void add (int u, int v) {
    edge[tot] = v;
    pre[tot] = head[u];
    head[u] = tot++;
}

void tarjan(int x) {
    low[x] = dfn[x] = ++now;
    Stack[++len] = x;
    inStack[x] = 1;
    for (int i = head[x]; i; i = pre[i]) {
        int y = edge[i];
        if (!dfn[y]) {
            tarjan(y);
            low[x] = min(low[x], low[y]);
        }else if (inStack[y]) low[x] = min(low[x], low[y]);
    }
    // for (int i = 0; i < (int)g[x].size(); ++i) {
        // int y = g[x][i];
        // if (!dfn[y]) tarjan(y), low[x] = min(low[x], low[y]);
        // else if (inStack[y])    low[x] = min(low[x], low[y]);
    // }
    if (dfn[x] == low[x]) {
        ++cnt;
        int top;
        while (Stack[len] != x) {
            top = Stack[len--];
            belong[top] = cnt;
            inStack[top] = 0; 
        }
        top = Stack[len--];
        belong[top] = cnt;
        inStack[top] = 0;
    }
}

int main() {
#ifndef ONLINE_JUDGE
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    n = read();
    for (int i = 1, m, d; i <= n; ++i) {
        m = read();
        while (m--) {
            d = read();
            // g[i].push_back(d+n);
            add(i, d+n);
        }
    }
    for (int i = 1, d; i <= n; ++i) {
        d = read();
        // g[d+n].push_back(i);
        add(d+n, i);
    }
    for (int i = 1; i <= n; ++i) {
        if (!dfn[i])  {
            tarjan(i);
        }

    }
    for (int i = 1; i <= n; ++i) {
        int sum = 0;
        for (int j = head[i], y; j; j = pre[j]) {
            y = edge[j];
            if (belong[y] == belong[i]) ans[++sum] = y;
        }
        // for (int j = 0, y; j < (int)g[i].size(); ++j) {
            // y = g[i][j];
            // if (belong[y] == belong[i]) ans[++sum] = y; 
        // }
        out(sum);
        sort(ans+1, ans+1+sum);
        for (int i = 1; i <= sum; ++i) {
            putchar(32); out(ans[i]-n);
        }
        putchar(10);
    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/henuyh/article/details/88540829
今日推荐