待改正

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Let_life_stop/article/details/83015066

题意:有2*n个骑士(二分图),要求你求一个集合,这个集合内的所有点之间没有边,且这个集合连出去的所有边能覆盖住出去这个集合外的所有点。(我们把最左边的点全部加入这个集合,然后把所有没有覆盖到的点都加入队列,然后依次处理队列中的元素,队列中的元素都要冲新加入集合,且它们连出的点都一定不再集合中,那么去除这些点又会新产生一些点没有被覆盖,我们再把它们加入队列依次循环。。。)

AC代码:

#include <iostream>
#include<cstring>
#include<queue>
#include<algorithm>
#include<stack>
#include<stdio.h>
#include<deque>
using namespace std;
# define maxn 200005
#define ll long long
int father[maxn];
int vis[maxn];
int du[maxn];
int main()
{
    int n;
    queue<int>q;
    scanf("%d",&n);
    memset(vis,0,sizeof(vis));
    memset(du,0,sizeof(du));
    for(int i=1; i<=2*n; i++)
    {
        scanf("%d",&father[i]);
    }
    for(int i=1; i<=n; i++)
    {
        vis[i]=1;
        du[father[i]]++;
    }
    for(int i=n+1; i<=2*n; i++)
    {
        if(du[i]==0)q.push(i);
    }
    while(!q.empty())
    {
        int temp=q.front();
        q.pop();
        if(du[temp])continue;
        vis[temp]=1;
        if(vis[father[temp]])
        {
            vis[father[temp]]=0;
            if(--du[father[father[temp]]]==0)
            {
                q.push(father[father[temp]]);
            }
        }
        else du[father[temp]]++;
    }
    for(int i=1; i<=2*n; i++)
    {
        if(vis[i])cout<<i<<" ";
    }
    cout<<endl;
    return 0;
}


猜你喜欢

转载自blog.csdn.net/Let_life_stop/article/details/83015066