南宁区域赛--The Maximum Unreachable Node Set--最大独立集+floyed处理联通性(省赛用到过处理联通性!!)

In this problem, we would like to talk about unreachable sets of a directed acyclic graph G = (V, E). In mathematics a directed acyclic graph (DAG) is a directed graph with no directed cycles. That is a graph such that there is no way to start at any node and follow a consistently-directed sequence of edges in E that eventually loops back to the beginning again. A node set denoted by VUR ⊂ V containing several nodes is known as an unreachable node set of G if, for each two different nodes u and v in VUR , there is no way to start at u and follow a consistently- directed sequence of edges in E that finally archives the node v. You are asked in this problem to calculate the size of the maximum unreachable node set of a given graph G. Input The input contains several test cases and the first line contains an integer T (1 ≤ T ≤ 500) which is the number of test cases. For each case, the first line contains two integers n (1 ≤ n ≤ 100) and m (0 ≤ m ≤ n(n−1)/2) indicating the number of nodes and the number of edges in the graph G. Each of the following m lines describes a directed edge with two integers u and v (1 ≤ u, v ≤ n and u ̸= v) indicating an edge from the u-th node to the v-th node. All edges provided in this case are distinct. We guarantee that all directed graphs given in input are DAGs and the sum of m in input is smaller than 500000. Output For each test case, output an integer in a line which is the size of the maximum unreachable node set of G. Sample Input 3 4 4 1 2 1 3 2 4 3 4 4 3 1 2 2 3 3 4 6 5 1 2 4 2 6 2 2 3 2 5 Sample Output 2 1 3

 

找出一个最大点集,使得任何两个点之间不能有边相连。求最大的点数。

 

很明显是最大独立集的定义,不过联通性没有给出全部,需要用floyed处理出所有点之间的联通性。

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=500+10;

struct Max_Match
{
    int n;
    int m;
    bool g[maxn][maxn];
    bool vis[maxn];
    int left[maxn];

    void init(int n,int m)
    {
        this->n=n;
        this->m=m;
        memset(g,0,sizeof(g));
        memset(left,-1,sizeof(left));
    }

    bool match(int u)
    {
        for(int v=1; v<=n; v++)
            if(g[u][v] && !vis[v])
            {
                vis[v]=true;
                if(left[v]==-1 || match(left[v]))
                {
                    left[v]=u;
                    return true;
                }
            }
        return false;
    }

    int solve()
    {
        for(int k=1; k<=n; k++)
        {
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=n; j++)
                {
                    g[i][j]=g[i][j]||(g[i][k]&&g[k][j]);
                }
            }
        }
        int ans=0;
        for(int i=1; i<=n; i++)
        {
            memset(vis,0,sizeof(vis));
            if(match(i))
                ans++;
        }
        return ans;
    }
} MM;
int main()
{
    int n,m,k,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        scanf("%d",&m);
        MM.init(n,m);
        while(m--)
        {
            int u,v,a;
            scanf("%d%d",&u,&v);
            MM.g[u][v]=true;
        }
        printf("%d\n",n-MM.solve());
    }
    return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值