Gym - 100712H Bridges

沉迷于连通分量了
最后一道了 ,o((>ω< ))o
不知道为什么乍看这道题感觉还挺简单,结果又是一个边连通分量+树的直径问题
树的直径已经被虐过一道了,真的难受T.T~

H. Bridges
An edge in an undirected graph is a ​ bridge​ if after removing it the graph will be disconnected.
Given an undirected connected graph, you are allowed to add one edge between any pair of nodes so that the total number of bridges in the graph is minimized.

Input
The first line of input contains ​ T (1 ≤ T ≤ 64) that represents the number of test cases.
The first line of each test case contains two integers: ​ N (3 ≤ N ≤ 100,000) and ​ M (N-1 ≤ M ≤ 100,000), where ​ N is the number of nodes, and ​ M is the number of edges.
Each of the following ​ M lines contains two space-separated integers: ​ X Y (1 ≤ X, Y ≤ N)(X ≠ Y) , which means that there is an edge between node ​ X
​ and node ​ Y.
It is guaranteed that each pair of nodes is connected by at most one edge.
Test cases are separated by a blank line.
Output
For each test case, print a single line with the minimum possible number of bridges after adding one edge.

Sample Input
2
7 7
1 2
2 3
3 1
3 4
4 5
4 6
6 7

3 3
1 2
2 3
3 1

Sample Output

1
0

这个题其实比上一道还要简单一点
只需要求出缩点后点的数量 - 树的直径的长度

#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
using namespace std;
const int maxn = 100010;
const LL inf = 0xffffffffff;
int n,m;
int dfn[maxn],low[maxn],belong[maxn];
int cnt,index;
struct node
{
    int u,v,w;
}s[maxn];
vectorG[maxn];
vector g[maxn];
stackS;
void init()
{
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(belong,0,sizeof(belong));
    for(int i=0;i<=n;i++)
    {
        G[i].clear();g[i].clear();
    }
    cnt = index = 0;
}
void Tarjan(int u,int pre)
{
    dfn[u] = low[u] = ++index;
    S.push(u);
    for(int i=0;i max_len) st = u,max_len = len;
    for(int i=0;i

你可能感兴趣的:(Gym - 100712H Bridges)