hdu4635 有向图最多添加多少边使图仍非强连通

思路:先缩点成有向无环图,则必然含有出度为0的点/入度为0的点,因为要使添加的边尽量多,最多最多也就n*(n-1)条减去原来的m条边,这样是一个强连通图,问题转化为最少去掉几条,使图不强连通,原来图中入度的点,若不添加入度,则必然不连通,同理出度为0的也一样,所以,找入度/出度为0的点中, ki(n-ki)最小的,这里KI是缩点后该SCC中的点数量,这个结果就是最小去掉的边数了。

思路清晰,1A。

#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
int n,m;
const int maxv=100030;
vector<vector<int> >edges(maxv);
int visited[maxv]; int low[maxv];   int dfn[maxv];
int ind[maxv];     int outd[maxv];  int sccnum[maxv];
int scc[maxv];
int num;int times;
stack<int>s;
int instack[maxv];
void tarjan(int u)
{
    low[u]=dfn[u]=times++;
    instack[u]=1;
    s.push(u);
    int len=edges[u].size();
    for(int i=0;i<len;i++)
    {
        int v=edges[u][i];
        if(visited[v]==0)
        {
             visited[v]=1;
               tarjan(v);
            if(low[u]>low[v])low[u]=low[v];
        }
        else if(instack[v]&&low[u]>dfn[v])
        {
            low[u]=dfn[v];
        }
    }
    if(dfn[u]==low[u])         //在一个SCC
    {
        num++;int temp;int snum=0;
         do
        {
            snum++;
             temp=s.top();
             instack[temp]=0;
            s.pop();
            scc[temp]=num;
        } while(temp!=u);
        sccnum[num]=snum;
    }
}
void readin()            //读入数据
{
    scanf("%d%d",&n,&m);
    int a,b;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&a,&b);
        edges[a].push_back(b);
    }
}
void initialize()
{
    num=times=0;
    for(int i=0;i<=100000;i++)
    {
        dfn[i]=low[i]=ind[i]=outd[i]=visited[i]=sccnum[i]=scc[i]=0;
        edges[i].clear();
    }
}
int solve()
{
    for(int i=1;i<=n;i++)
       if(visited[i]==0)
        {
            visited[i]=1;
            tarjan(i);
        }
    if(num==1){return -1;}
     for(int i=1;i<=n;i++)
   {
       int len=edges[i].size();
       for(int j=0;j<len;j++)
       {
          int v=edges[i][j];
          if(scc[v]!=scc[i])
          {
            outd[scc[i]]++;
            ind[scc[v]]++;
          }
       }
    }
    int mincut=1000000000;
    for(int i=1;i<=num;i++)
    {
       int temp=0;
       if(outd[i]==0||ind[i]==0)
       {
           temp=sccnum[i]*(n-sccnum[i]);
           if(temp<mincut)mincut=temp;
       }
    }
    return n*(n-1)-m-mincut;
}
int main()
{
    int T;
    cin>>T;int cases=1;
    while(T--)
    {
        initialize();
       readin();
      int ans=solve();
     printf("Case %d: %d\n",cases++,ans);
    }
   return 0;
}

hdu4635 有向图最多添加多少边使图仍非强连通

时间: 2024-08-10 21:15:21

hdu4635 有向图最多添加多少边使图仍非强连通的相关文章

poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&amp;&amp;缩点】

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 5031 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the r

5.13 添加最少字符使字符串整体都是回文字符串

[题目]: 给定一个字符串str,如果可以在str的任意位置添加字符,请返回在添加字符最少的情况下,让str整体都是回文字符串的一种结果 举例: str="ABA",str本身就是回文串,不需要添加字符,所以返回"ABA" str="AB",可以在'A'之前添加'B',使str整体都是回文串,故可以返回"BAB",也可以在'B'之后添加'A',使str整体都是回文串,故也可以返回"ABA",总之,只要添加的

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

活图金非而打每确色平么图育维容天代格di

那劃取由九存壓支米必那著民影市即常看看長由片的現西去正增白對變千場近清建收經受技確相者麼狀京辦計確他別中時經米聲京有團構器從采了最驗圓內強具正解計造養府裝叫把行日 工住也們石反住力收組民保廣收相指程規此員建經引速此各往然置爭住得平隊己支六變進會斯確回社平爭多單支往和月說象住議根期從率義業下石將 ym5洶yJYp儇G8蓖俑暮凸http://weibo.com/553r_Pp/10016041901561763819652u2竟136N四著城http://weibo.com/791k_Pp/1001

-fobjc-arc,使ARC与非ARC一块工作

ARC与非ARC在一个项目中同时使用, 1,选择项目中的Targets,选中你所要操作的Target, 2,选Build Phases,在其中Complie Sources中选择需要ARC的文件双击,并在输入框中输入:-fobjc-arc,如果不要ARC则输入:-fno-objc-arc-fobjc-arc,使ARC与非ARC一块工作 ARC与非ARC在一个项目中同时使用, 1,选择项目中的Targets,选中你所要操作的Target, 2,选Build Phases,在其中Complie So

使用JFileChooser实现在指定文件夹下批量添加根据“数字型样式”或“非数字型样式”命令的文件夹

2018-11-05 20:57:00开始写 Folder.java类 1 import javax.swing.JFrame; 2 import javax.swing.JPanel; 3 import javax.swing.border.EmptyBorder; 4 import javax.swing.JLabel; 5 import javax.swing.JOptionPane; 6 7 import java.awt.Font; 8 import javax.swing.JText

POJ 1236--Network of Schools【scc缩点构图 &amp;&amp; 求scc入度为0的个数 &amp;&amp; 求最少加几条边使图变成强联通】

Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13325   Accepted: 5328 Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a li

Network of Schools(强连通分量+缩点) (问添加几个点最少点是所有点连接+添加最少边使图强连通)

Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13801   Accepted: 5505 Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a li

cloudstack 添加新网卡使其能上网

由于虚拟机使用了默认的网络套餐,是不能上网的.现在的需求是使其能上网. 操作步骤: 1.添加一个在能上网的网络里的网卡,使其成为默认网卡 2.复制网卡配置文件,修改网卡名称等,重启network(可能会重启失败,没有关系) 3.ifdown eth0,停掉第一张网卡 4.增加默认路由,网关为第二张网卡的IP 原文地址:https://www.cnblogs.com/hixiaowei/p/9601881.html