UVALive 5962 Strongly Connected Chemicals --最大独立集

题意:给n个阳离子和m个阴离子,并给出相互的吸引关系,求一个最大的点集,使其中的每个阴阳离子相互吸引。

解法:枚举每条边,使该条边存在,然后建立反图,求一个最大匹配,此时的点数减去最大匹配与ans求一个最大值即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
using namespace std;
#define N 207

int G[N][N],G2[N][N];
int match[N];
int vis[N];
char ss[N][N];
int n,m;

int Search_Path(int s)
{
    for(int v=0;v<m;v++)
    {
        if(G[s][v] == 0)
            continue;
        if(!vis[v])
        {
            vis[v] = 1;
            if(match[v] == -1 || Search_Path(match[v]))
            {
                match[v] = s;
                return 1;
            }
        }
    }
    return 0;
}

int Max_match()
{
    memset(match,-1,sizeof(match));
    int cnt = 0;
    for(int i=0;i<n;i++)
    {
        memset(vis,0,sizeof(vis));
        if(Search_Path(i))
            cnt++;
    }
    return cnt;
}

int main()
{
    int t,cs = 1,i,j,k,h;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(i=0;i<n;i++)
        {
            scanf("%s",ss[i]);
            for(j=0;j<m;j++)
                G[i][j] = ss[i][j] - ‘0‘;
        }
        int ans = 0;
        for(i=0;i<n;i++)  //枚举边
        {
            for(j=0;j<m;j++)
            {
                if(G[i][j])
                {
                    int cntn = 0;
                    int cntm = 0;
                    for(k=0;k<n;k++)
                        if(G[k][j])
                            cntn++;
                    for(h=0;h<m;h++)
                        if(G[i][h])
                            cntm++;
                    for(k=0;k<n;k++)
                    {
                        for(h=0;h<m;h++)
                        {
                            if(G[k][j]&&G[i][h])
                                G2[k][h] = 1-G[k][h];
                            else
                                G2[k][h] = 0;
                        }
                    }
                    ans = max(ans,cntn+cntm-Max_match());
                }
            }
        }
        printf("Case %d: %d\n",cs++,ans);
    }
    return 0;
}

时间: 2024-10-05 05:25:06

UVALive 5962 Strongly Connected Chemicals --最大独立集的相关文章

Light OJ 1373 Strongly Connected Chemicals 二分匹配最大独立集

m种阳离子 n种阴离子 然后一个m*n的矩阵 第i行第j列为1代表第i种阴离子和第j种阴离子相互吸引 0表示排斥 求在阳离子和阴离子都至少有一种的情况下 最多存在多少种离子可以共存 阴阳离子都至少需要存在一种 那么可以枚举哪2种离子共存 假设枚举a b 然后找到所有的和a可以共存的阴离子(设为x集合)以及和b共存的阳离子(设为y集合) 现在只需求x集合中和y集合中最多有多少个离子可以共存 这个求最大独立集就行了 枚举所有的a b 取最大值 #include <cstdio> #include

PTA Strongly Connected Components

Write a program to find the strongly connected components in a digraph. Format of functions: void StronglyConnectedComponents( Graph G, void (*visit)(Vertex V) ); where Graph is defined as the following: typedef struct VNode *PtrToVNode; struct VNode

HDU 4635 —— Strongly connected——————【 强连通、最多加多少边仍不强连通】

Strongly connected Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4635 Description Give a simple directed graph with N nodes and M edges. Please tell me the maximum number of the edges you can

HDU 4635 Strongly connected (有向图的强连通分量)

Strongly connected Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Give a simple directed graph with N nodes and M edges. Please tell me the maximum number of the edges you can add that the grap

[tarjan] hdu 4635 Strongly connected

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4635 Strongly connected Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1568    Accepted Submission(s): 654 Problem Description Give a simple dir

HDU 4635 Strongly connected(强连通)经典

Strongly connected Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1828    Accepted Submission(s): 752 Problem Description Give a simple directed graph with N nodes and M edges. Please tell me

[email&#160;protected] Strongly Connected Component

Strongly Connected Components A directed graph is strongly connected if there is a path between all pairs of vertices. A strongly connected component (SCC) of a directed graph is a maximal strongly connected subgraph. For example, there are 3 SCCs in

Strongly connected

hdu4635:http://acm.hdu.edu.cn/showproblem.php?pid=4635 题意:给你一个有向图,然后问你最多可以加多少条边,是的原图不是一个强连通图. 题解:这一题确实不会,图论做的太少了,一下是一个人分析,觉得分析的很不错,代码也是看别人的. 首先强连通缩点,缩点之后,最终添加完边的图,肯定可以分成两个部X和Y,其中只有X到Y的边没有Y到X的边;  *那么要使得边数尽可能的多,则X部肯定是一个完全图,Y部也是,同时X部中每个点到Y部的每个点都有一条边;  *

【CodeForces】913 F. Strongly Connected Tournament

[题目]F. Strongly Connected Tournament [题意]给定n个点(游戏者),每轮游戏进行下列操作: 1.对于游戏者i和j(i<j),有p的概率i赢j(反之j赢i),连边从赢者向输者,从而得到一个有向完全图,这些点视为进行了一轮游戏. 2.对于其中点数>1的强连通分量再次进行过程1,直至不存在点数>1的强连通分量为止. 给定n和p,求所有点进行的游戏轮数之和,2<=n<=2000. [算法]数学概率,期望DP [题解]参考:官方题解Hello 201