QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#453757#852. JellyfishNana7AC ✓100ms18240kbC++141.7kb2024-06-24 10:50:052024-06-24 10:50:05

Judging History

你现在查看的是最新测评结果

  • [2024-06-24 10:50:05]
  • 评测
  • 测评结果:AC
  • 用时:100ms
  • 内存:18240kb
  • [2024-06-24 10:50:05]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#define I inline
using namespace std;

const int N = 100010;
int siz[N],vis[N],cir[N],pre[N],inc[N],ccnt;
int n,flag;
vector<int> q[N];

void dfs1(int x,int fa) {
	pre[x]=fa; vis[x]=1;
	for(auto &t:q[x] ) {
		if(flag) return ;
		if(t==fa) continue;
		if(vis[t]) {
			int mk=x; flag=1;
			while(1) {
			//	cout<<mk<<' '<<pre[mk]<<endl;
				cir[++ccnt]=mk;
				inc[mk]=1;
				if(mk==t) break;
				mk=pre[mk];
			}
			return ;
		}
		dfs1(t,x);
	}
}
void dfs2(int x,int fa) { //cout<<x<<endl;
	if(q[x].size()==1) {
		siz[x]=1;
		return ;
	}
	for(auto &t:q[x]) {
		if(t==fa||inc[t]) continue;
		dfs2(t,x);
		siz[x]+=siz[t];
	}
}
I void clear() {
	ccnt=flag=0;
	for(int i=1;i<=n;++i) {
		q[i].clear();
		siz[i]=vis[i]=cir[i]=pre[i]=inc[i]=0;
	}
}
I void check1() {
	cout<<ccnt<<endl;
	for(int i=1;i<=ccnt;++i) cout<<cir[i]<<' '; cout<<endl;
}
I int read() {
	int ret=0,w=1; char ch;
	while((ch=getchar())>'9'||ch<'0'&&ch!='-'); if(ch=='-') w=-1; else ret=ch-'0';
	while((ch=getchar())>='0'&&ch<='9') ret=ret*10+ch-'0';
	return ret*w; 
}
int main()
{
	int T; cin>>T;
	while(T--) {
		n=read(); clear();
		for(int i=1;i<=n;++i) {
			int x,y; x=read(); y=read();
			q[x].push_back(y);
			q[y].push_back(x);
		}		
		dfs1(1,0);
	//	cout<<"waao"<<endl;
	//	check1();
		for(int i=1;i<=ccnt;++i) dfs2(cir[i],0);
		int sum=0,ans=3;
		for(int i=1;i<=ccnt;++i) sum+=siz[cir[i]];
		for(int i=1;i<=ccnt;++i) ans=max(ans,sum-siz[cir[i]]+1);
		for(int i=1;i<=ccnt;++i) {
			int l=cir[i],r=cir[i%ccnt+1];
			ans=max(ans,sum-siz[l]-siz[r]+2);
		}
		cout<<max(ans,sum)<<endl;
	}
	
 } 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 6588kb

input:

2
6
1 2
2 3
3 4
4 1
2 5
2 6
4
1 2
2 3
3 4
4 1

output:

4
3

result:

ok 2 number(s): "4 3"

Test #2:

score: 0
Accepted
time: 100ms
memory: 6464kb

input:

85665
6
3 2
4 1
4 6
2 1
2 6
5 1
7
6 2
6 3
5 1
1 2
2 4
4 7
3 7
7
6 1
6 7
1 4
1 3
7 5
5 3
4 2
7
6 2
7 4
7 5
3 1
3 4
2 5
1 4
7
7 2
2 6
5 4
5 6
5 1
3 1
4 6
7
3 5
3 1
3 7
3 2
5 1
5 4
4 6
7
4 5
4 1
3 6
3 7
6 7
6 1
2 1
7
5 3
7 3
1 4
6 2
6 3
2 3
4 3
7
2 3
2 6
2 4
7 5
3 5
5 1
1 4
7
3 4
3 7
5 6
2 7
4 6
6 7
6 ...

output:

4
3
3
3
3
4
4
5
4
5
4
4
3
4
4
3
4
4
4
4
4
5
3
4
3
4
3
9
4
4
3
4
8
3
98
5
4
3
6
4
4
4
4
3
4
4
4
4
5
3
5
4
3
4
95
4
4
4
5
4
3
4
3
5
4
3
4
3
3
4
4
4
4
4
3
4
4
4
3
3
3
4
4
3
4
4
4
4
4
4
3
3
5
5
4
5
4
3
4
4
3
3
3
5
4
4
4
6
4
5
5
5
4
3
5
4
4
3
4
10
4
3
3
4
4
3
5
4
4
3
5
3
4
4
3
3
3
4
5
98
5
105
4
4
4
3
4
...

result:

ok 85665 numbers

Test #3:

score: 0
Accepted
time: 83ms
memory: 18240kb

input:

30
2229
1066 248
248 881
881 2080
2080 1615
1615 1647
1647 1774
1774 196
196 434
434 390
390 129
129 563
563 63
63 1457
1457 1015
1015 2200
2200 1187
1187 1763
1763 1121
1121 2122
2122 1783
1783 1756
1756 2031
2031 2153
2153 605
605 1778
1778 1287
1287 2062
2062 817
817 194
194 474
474 414
414 1736
...

output:

1092
881
722
1412
37556
638
438
509
273
29198
740
27535
46011
865
444
30031
49564
794
489
469
624
956
1180
17384
50000
715
1291
49920
1465
3

result:

ok 30 numbers