QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#453743#852. JellyfishNana7WA 57ms7832kbC++141.7kb2024-06-24 10:20:382024-06-24 10:20:39

Judging History

This is the latest submission verdict.

  • [2024-06-24 10:20:39]
  • Judged
  • Verdict: WA
  • Time: 57ms
  • Memory: 7832kb
  • [2024-06-24 10:20:38]
  • Submitted

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[i]+1);
		for(int i=1;i<=ccnt;++i) {
			int l=i,r=i%ccnt+1;
			ans=max(ans,sum-siz[l]-siz[r]+2);
		}
		cout<<ans<<endl;
	}
	
 } 

详细

Test #1:

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

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: -100
Wrong Answer
time: 57ms
memory: 7148kb

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:

3
3
3
3
3
4
3
4
4
5
4
3
3
3
3
3
3
4
4
3
3
4
3
3
3
3
3
9
4
3
3
3
7
3
97
4
4
3
6
3
4
3
4
3
4
4
4
3
4
3
3
3
3
3
95
3
4
3
4
3
3
3
3
4
3
3
4
3
3
3
4
3
3
3
3
3
4
4
3
3
3
3
3
3
3
3
3
4
3
3
3
3
4
4
4
4
4
3
3
3
3
3
3
4
3
3
3
5
4
4
4
4
3
3
3
4
4
3
3
10
3
3
3
3
3
3
5
4
4
3
4
3
3
4
3
3
3
4
4
97
4
104
4
3
3
3
3
...

result:

wrong answer 1st numbers differ - expected: '4', found: '3'