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. Nginx启动报错误unlink() “nginx.pid” failed (2: No such file or directory)

    在修改了配置文件之后,启动nginx的时候发现,进程已经存在了,但是端口没有监听到. 查看错误日志,有如下错误信息 2015/10/22 21:39:06 [alert] 26434#0: unlin ...

  2. 阿里云ECS发送邮件失败

    阿里云发送SMTP邮件失败   N多测试发现 阿里云服务器不能用25端口发邮件,配置465端口阿里云发送邮件是成功的 修改mail.rc 里的smtp 端口为465 配置如下 set from=*** ...

  3. python多线程,event,互斥锁,死锁,递归锁,信号量

    Python多线程/event 多线程-threading python的thread模块是⽐较底层的模块, python的threading模块是对thread做了⼀些包装的, 可以更加⽅便的被使⽤ ...

  4. Helvetic Coding Contest 2018 online mirror (teams allowed, unrated)F3 - Lightsabers (hard)

    题意:n个数字1-m,问取k个组成的set方案数 题解:假设某个数出现k次,那么生成函数为\(1+x+...+x^k\),那么假设第i个数出现ai次,结果就是\(\sum_{i=1}^m(1+x+.. ...

  5. TopCoder[SRM513 DIV 1]:Reflections(1000)

    Problem Statement      Manao is playing a new game called Reflections. The goal of the game is trans ...

  6. Linux课程---14、linux下lamp环境如何安装

    Linux课程---14.linux下lamp环境如何安装 一.总结 一句话总结: 要按顺序安装,比如apache需要在php之前安装, 一.安装 gcc 编译器 二.卸载 rpm 安装的 http ...

  7. JAVA基础_类加载器

    什么是类加载器 类加载器是Java语言在1.0版本就引入的.最初是为了满足JavaApplet需要.现在类加载器在Web容器和OSGI中得到了广泛的应用,一般来说,Java应用的开发人员不需要直接同类 ...

  8. C++开发系列-内联函数

    内联函数 C++使用内联函数来替代宏代码片段. #include <iostream> int main(){ printfA(); return 0; } inline void pri ...

  9. 2018-2-13-安装visualStudio-出现-cant-install-Microsoft.TeamFoundation.OfficeIntegration.Resources...

    title author date CreateTime categories 安装visualStudio 出现 cant install Microsoft.TeamFoundation.Offi ...

  10. NuGet 命令行使用EntityFrameWork

    初始化 Enable-migrations 迁移 Add-Migration Donator_Add_CreationTime 执行操作 UpDate-database 撤销更改 Update-Dat ...