POJ1904 King‘s Quest(scc缩点+思维)

题目链接
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个儿子要娶公主,给出每个儿子心仪的公主编号,并给出最后每个儿子娶了哪个公主的编号。求解每个儿子的可能迎娶的公主集合,该集合要保证某个王子娶了里面的公主以后,剩下的王子一定都还是能娶到老婆(即不改变最大匹配)

分析
若一个有向图内,点U,V处于同一个联通分量,那么意味着我们删掉一个V以后,该联通分量内仍然满足任意的王子可以至少走到一个公主的点。换句话说,初始王子喜欢公主连边,最终的公主嫁给谁连边,做scc缩点以后判断同一个联通分量内的公主王子即可。

#include <bits/stdc++.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <time.h>
#include <cstdio>
#include <iostream>
#include <vector>
#define ll long long
#define int long long
#define inf 0x3f3f3f3f
#define mods 1000000007
#define modd 998244353
#define PI acos(-1)
#define fi first
#define se second
#define lowbit(x) (x & (-x))
#define mp make_pair
#define pb push_back
#define si size()
#define E exp(1.0)
#define fixed cout.setf(ios::fixed)
#define fixeds(x) setprecision(x)
#define Re register int
using namespace std;
 ll gcd(ll a,ll b){if(a<0)a=-a;if(b<0)b=-b;return b==0?a:gcd(b,a%b);}
template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true);
for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);}
const int N=1e5,M=4e5;
int n;
int x,y,Q_o,A[N],ru[N],gs[N],ip[N];
vector<ll>scc[4020];
struct Tarjan{
    int o,t,dfn_o,Q[N],low[N],dfn[N],pan[N],head[N];
    struct QAQ{int x,to,next;}a[M];
    inline void add(Re x,Re y){a[++o].x=x,a[o].to=y,a[o].next=head[x],head[x]=o;}
    inline void tarjan(Re x){
        dfn[x]=low[x]=++dfn_o,Q[++t]=x,pan[x]=1;
        for(Re i=head[x],to;i;i=a[i].next)
            if(!dfn[to=a[i].to])tarjan(to),low[x]=min(low[x],low[to]);
            else if(pan[to])low[x]=min(low[x],dfn[to]);
        if(low[x]==dfn[x]){
            ++Q_o;
            while(1){
                    scc[Q_o].push_back(Q[t]);
                ip[Q[t]]=Q_o,gs[Q_o]+=A[Q[t]],pan[Q[t]]=0;
                if(x==Q[t--])break;
            }
        }
    }
    inline void SuoPoint(){for(Re i=1;i<=n*2;++i)if(!dfn[i])tarjan(i);}
}T1;
struct Tuopu{
    int o,ru[N],pan[N],head[N];std::queue<int>Q;
    struct QAQ{int to,next;}a[M<<1];
    inline void add(Re x,Re y){a[++o].to=y,a[o].next=head[x],head[x]=o;}
    inline void creat(){
        for(Re i=1;i<=T1.o;++i)
            if((x=ip[T1.a[i].x])!=(y=ip[T1.a[i].to]))
                ++ru[y],add(x,y);
    }

}T2;
vector<ll>ANS[2010];
ll wtf[2200];
ll vis[2100][4100];
signed main(){
    read(n);
    for(int i=1;i<=n;i++){
            A[i]=A[i+n]=1;

    ll num;
    read(num);
    for(int j=1;j<=num;j++){
    ll x;
    read(x);
    T1.add(i,x+n);
    vis[i][x+n]=1;
    }

    }
    for(int i=1;i<=n;i++){

      read(wtf[i]);
      T1.add(wtf[i]+n,i);
    }

  T1.SuoPoint(),T2.creat();

   for(int i=1;i<=Q_o;i++){
    ll ve=scc[i].size();
    for(int j=0;j<ve;j++){

    for(int k=0;k<ve;k++){
      if(j==k)continue;
      if(scc[i][j]>n&&scc[i][k]<=n&&vis[scc[i][k]][scc[i][j]]==1){
      ANS[scc[i][k]].push_back(scc[i][j]-n);

      }

    }

    }
   }

for(int i=1;i<=n;i++){
        sort(ANS[i].begin(),ANS[i].end());
  printf("%d",ANS[i].size());

    for(int j=0;j<ANS[i].size();j++){
    printf(" %lld",ANS[i][j]);

    }
    printf("\n");
}



}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值