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. Redis中取出值,转成对象

    import com.fasterxml.jackson.databind.ObjectMapper; //转成companyEntity CompanyEntity company = mapper ...

  2. hdu多校第三场 1006 (hdu6608) Fansblog Miller-Rabin素性检测

    题意: 给你一个1e9-1e14的质数P,让你找出这个质数的前一个质数Q,然后计算Q!mod P 题解: 1e14的数据范围pass掉一切素数筛法,考虑Miller-Rabin算法. 米勒拉宾算法是一 ...

  3. 从 i++ 和 ++i 说起局部变量表和操作数栈

    本文转载自:从 i++ 和 ++i 说起局部变量表和操作数栈 最近公司有人看了尚硅谷柴林燕老师的第一季面试题,就想来考考我.我觉得柴老师讲的很好,部分内容可以延伸一下,所以写这篇文章分享给大家! 这篇 ...

  4. day 60 Django基础七之Ajax

      Django基础七之Ajax   本节目录 一 Ajax简介 二 Ajax使用 三 Ajax请求设置csrf_token 四 关于json 五 补充一个SweetAlert插件(了解) 六 同源策 ...

  5. 第四周——重新clone项目后maven问题

    重新clone项目后,一直报错,"类重复..." clean后install也无效果. 原因是idea在重启项目时会更改maven为默认的idea自带的maven配置,要重新设置

  6. Django之单表查询——神奇的双下划线

    1.filter中的单表查询 # 查询id>1且id<4的结果 ret = models.Person.objects.filter(id__gt=1,id__lt=4) print(re ...

  7. nginx实用配置用例

    vue项目部署及后台api访问 nginx.conf # vue本地项目配置 ... server { listen 8000; server_name localhost; root /.../di ...

  8. 解决asp.net web api时间datetime自动带上带上的T和毫秒的问题

    今天用asp.net web api写微信小程序的接口时遇到一个问题. 返回的model中的datetime类型的字段自动转换成了“2014-11-08T01:50:06:234”这样的字符串,带上的 ...

  9. NoSQL SimpleDB

  10. 乌云精华漏洞整合(epub)

    还是7月份的 链接: http://pan.baidu.com/s/1kUGIOez 密码: gfqp