Gym 100712H
https://vjudge.net/problem/195715/origin
先缩点,再建立新图,然后跑两遍dfs求树上最长路

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#define inf 2147483647
#define ls rt<<1
#define rs rt<<1|1
#define lson ls,nl,mid,l,r
#define rson rs,mid+1,nr,l,r
#define N 200010
#define For(i,a,b) for(int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar() using namespace std; int T;
int n,m,x,y,cnt,col,now,S,Max;
int dfn[N],low[N],c[N],ans;
bool vis[N];
struct node{
int n;
node *next;
}*e[N];
stack<int>s;
struct edge{
int x;int y;
}a[N];
void in(int &x){
int y=;
char c=g();x=;
while(c<''||c>''){
if(c=='-')y=-;
c=g();
}
while(c<=''&&c>=''){
x=(x<<)+(x<<)+c-'';c=g();
}
x*=y;
}
void o(int x){
if(x<){
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
} void push(int x,int y){
node *p;
p=new node();
p->n=y;
if(e[x]==)
e[x]=p;
else{
p->next=e[x]->next;
e[x]->next=p;
}
} void tarjan(int x,int fa){
dfn[x]=low[x]=++cnt;
vis[x]=;
s.push(x);
for(node *i=e[x];i;i=i->next){
if(!dfn[i->n]){
tarjan(i->n,x);
low[x]=min(low[x],low[i->n]);
}
else
if(vis[i->n]&&i->n!=fa)
low[x]=min(low[x],dfn[i->n]);
}
if(low[x]==dfn[x]){
col++;
do{
ans++;
now=s.top();
c[now]=col;
s.pop();
vis[now]=0;
}while(x!=now);
}
} void dfs(int x,int fa,int deep){
if(Max<deep){
Max=deep;
S=x;
}
for(node *i=e[x];i;i=i->next)
if(i->n!=fa)
dfs(i->n,x,deep+);
} void clear(){
memset(vis,,sizeof(vis));
memset(dfn,,sizeof(dfn));
memset(c,,sizeof(c));
memset(low,,sizeof(low));
memset(e,,sizeof(e));
Max=;
col=;
cnt=;
} int main(){
in(T);
while(T--){
clear();
in(n);in(m);
For(i,,m){
in(x);in(y);
a[i].x=x;
a[i].y=y;
push(x,y);
push(y,x);
}
tarjan(,);
memset(e,,sizeof(e));
For(i,,m)
if(c[a[i].x]!=c[a[i].y]){
push(c[a[i].x],c[a[i].y]);
push(c[a[i].y],c[a[i].x]);
}
dfs(,,);
Max=;
dfs(S,S,);
o(col-Max-);p('\n');
}
return ;
}

Gym 100712H的更多相关文章

  1. Bridges Gym - 100712H  无向图的边双连通分量,Tarjan缩点

    http://codeforces.com/gym/100712/attachments 题意是给定一个无向图,要求添加一条边,使得最后剩下的桥的数量最小. 注意到在环中加边是无意义的. 那么先把环都 ...

  2. Gym - 100712H Bridges(边—双连通分量)

    https://vjudge.net/problem/Gym-100712H 题意: 给出一个图,求添加一条边后最少的桥数量. 思路: 参考了ZSQ大神的题解http://blog.csdn.net/ ...

  3. 18春季训练01-3/11 2015 ACM Amman Collegiate Programming Contest

    Solved A Gym 100712A Who Is The Winner Solved B Gym 100712B Rock-Paper-Scissors Solved C Gym 100712C ...

  4. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

  5. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  6. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  7. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

  8. Gym 101102J---Divisible Numbers(反推技巧题)

    题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...

  9. Gym 100917J---Judgement(01背包+bitset)

    题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...

随机推荐

  1. uoj49 轴仓库

    题意: n叠箱子排成一线,第i叠箱子坐标为xi,竖直方向叠着ai个箱子. 可以花费+1s左移或右移一位,也可以在瞬间搬起一个位置的箱子,或将怀里的有且仅有一个箱子放下. 任意选择起点s(可以不与xi重 ...

  2. 将文件大小数值转换成B、KB、MB、GB

    //delphi 将文件大小数值转换成B/KB/MB/GB function FormatByteSize(const bytes: Longint): string; const B = 1; // ...

  3. 小程序修改默认的radio样式

    1.wxml: <radio-group class="radio-group" bindchange="radioChange"> <vie ...

  4. Android Drawable 详解(教你画画!)

    参考 1.Android中的Drawable基础与自定义Drawable 2.android中的drawable资源 3.Android开发之Shape详细解读 Drawable分类 No xml标签 ...

  5. python的__file__和__name__变量

    #现在的目录结构为 #现在想要在web2/bin.py中调用web3/main.py模块中的方法 from web3 import main main.foo() #在pycharm中执行 ##### ...

  6. Sequelize

    连接数据库 const DB = require('sequelize') // 连接数据库 const connect = new DB('xjg', 'root', 'root', { host: ...

  7. Postman提取返回值

    json响应结果 Postman是做接口测试的,但是很多接口并不是直接就能测试的,需要一些预处理.比如登录的时候,需要传递一个token.如果是网页测试,一般打开登录页面的时候就会自动生成一个toke ...

  8. mybatis-plus分页查询

    在springboot中整合mybatis-plus 按照官方文档进行的配置:快速开始|mybatis-plus 引入依赖: <!-- 引入mybatisPlus --> <depe ...

  9. POJ 3376 Finding Palindromes EX-KMP+字典树

    题意: 给你n个串串,每个串串可以选择和n个字符串拼接(可以自己和自己拼接),问有多少个拼接后的字符串是回文. 所有的串串长度不超过2e6: 题解: 这题由于是在POJ上,所以string也用不了,会 ...

  10. WPF drag过程中显示ToolTip.

    原文:WPF drag过程中显示ToolTip. 在drag/drop过程中,我们在判断出over的元素上是否可以接受drag的东西之后,通常是通过鼠标的样式简单告诉用户这个元素不接受现在drag的内 ...