Bayan 2015 Contest Warm Up E题(双连通分量缩点+DP计数)

E. Strongly Connected City 2
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Imagine a city with n junctions and m streets. Junctions are numbered from 1 to n.

In order to increase the traffic flow, mayor of the city has decided to make each street one-way. This means in the street between junctions u and v, the traffic moves only from u to v or only from v to u.

The problem is to direct the traffic flow of streets in a way that maximizes the number of pairs (u, v) where 1 ≤ u, v ≤ n and it is possible to reach junction v from u by passing the streets in their specified direction. Your task is to find out maximal possible number of such pairs.

Input

The first line of input contains integers n and m, (), denoting the number of junctions and streets of the city.

Each of the following m lines contains two integers u and v, (u ≠ v), denoting endpoints of a street in the city.

Between every two junctions there will be at most one street. It is guaranteed that before mayor decision (when all streets were two-way) it was possible to reach each junction from any other junction.

Output

Print the maximal number of pairs (u, v) such that that it is possible to reach junction v from u after directing the streets.

Sample test(s)
input
5 4
1 2
1 3
1 4
1 5
output
13
input
4 5
1 2
2 3
3 4
4 1
1 3
output
16
input
2 1
1 2
output
3
input
6 7
1 2
2 3
1 3
1 4
4 5
5 6
6 4
output
27
Note

In the first sample, if the mayor makes first and second streets one-way towards the junction 1 and third and fourth streets in opposite direction, there would be 13 pairs of reachable junctions: {(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (2, 1), (3, 1), (1, 4), (1, 5), (2, 4), (2, 5), (3, 4), (3, 5)}


题意:给出一个无向图,要求把每条边都变成单向的,然后使得能够到达的点对最多

思路:首先不难想到要用双连通分量缩点,缩完点以后就是一棵树了

            然后暴力以每个点(注意这里已经是缩点之后的点)为树根进行DFS,边DFS边计数即可

            还要注意一种情况,就是根的子树可以分为两部分num1 和 num2,最后的答案要累加上num1 * num2,意味着num1的所有点都可以达到num2,这个用背包求即可

            细节看代码吧~

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
        
          #include 
         
           using namespace std; typedef long long ll; const int MAXN = 2010; vector 
          
            head[MAXN],cphead[MAXN]; stack 
           
             s; int dfs_clock; int pre[MAXN]; int id[MAXN]; int low[MAXN]; int components; int sz[MAXN]; void tarjan(int u,int p) { pre[u]=low[u]=++dfs_clock; s.push(u); int L=head[u].size(); for(int i=0;i 
            
              & q) { int L=q.size(); int to=0; for(int i=0;i 
             
               =q[i];--j){ f[j]|=f[j-q[i]]; } } int ans=0; for(int i=1;i<=to;i++){ if(f[i])ans=max(ans,i*(to-i)); } return ans; } int main() { int n,m; scanf("%d%d",&n,&m); for(int i=0;i 
              
                =id[v])continue; cphead[id[i]].push_back(id[v]); cphead[id[v]].push_back(id[i]); } } int ans=0; for(int i=1;i<=components;i++){ int L=cphead[i].size(); tot[i]=sz[i]*sz[i]; num[i]=sz[i]; vector 
               
                 ss; for(int j=0;j 
                 
                
               
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值