首页 技术 正文
技术 2022年11月23日
0 收藏 816 点赞 2,650 浏览 1699 个字

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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,997
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,356
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,139
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848