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. Oracle18C安装后首次创建数据库并用sql developer 创建连接和用户

    注意: SQL Developer 不能用于创建Oracle数据库,只能用来连接已经创建的数据库,数据库的建立要通过Database Configuration Assistant(DBCA)来完成. ...

  2. java String,StringBuilder和StringBuffer类的区别

    对String而言: String是只读字符串,所引用的字符串不能被改变,一经定义,无法再增删改. String 定义的字符串保存在常量池里面,进行+操作时不能直接在原有基础上拼接. 每次+操作 : ...

  3. 利用VS 性能探查器 解决代码性能不高问题

    VS2017 分析-性能探查器 选择你想分析的项目,选择你的分析工具,我这边是遇到了一个cpu爆满的问题 启动后需要点击一下记录cpu,否则会没有后续的分析忘了截图了,下面是出的分析报告,分析时间比较 ...

  4. matplotlib实现伪彩色图像和色度条的展现

    灰度图显示为伪彩色图 法一 import matplotlib.pyplot as plt img = plt.imread('C:/Users/leex/Desktop/lena.jpg') img ...

  5. Spring Cloud失散多年的哥哥Dubbo学习笔记

    Spring Cloud失散多年的哥哥Dubbo 随着互联网项目用户量的急剧增长,访问并发良突然暴增,将一个应用使用多个独立的工程共同实现的系统架构,称为SOA系统架构,各个工程可以允许在不同的机器上 ...

  6. redis可视化客户端工具TreeNMS

    TreeNMS是一款redis,Memcache可视化客户端工具,采用JAVA开发,实现基于WEB方式对Redis, Memcached数据库进行管理.维护. 功能包括:状态参数监控,NoSQL数据库 ...

  7. 命令学习_nslookup

    nslookup 域名 这是最常用最简单的用法,可以直接获得目标域名的IP地址和CNAME. 如下是A记录的返回情况 nslookup命令会采用先反向解释获得使用的DNS服务器的名称,上图中ns.gu ...

  8. mysql的建表约束

    主键约束(primary key) 主键约束能够唯一确定一张表中的记录,也就是可以通过某个字段添加约束,就可以是的该字段不重复,且不为空 create table user (id int prima ...

  9. 2019-8-31-dotnet-通过-WMI-获取系统启动的服务

    title author date CreateTime categories dotnet 通过 WMI 获取系统启动的服务 lindexi 2019-08-31 16:55:59 +0800 20 ...

  10. php非法输入数据类型

    1.空白输入 2.超长输入(如大于256个字符) 3.特殊字符(如·!@#¥%……&*()—=|.:‘:<>;'"<>?.,) 4.控制字符(如\r\n等) ...