QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#499548#6729. Unrooted TriezmrzmrTL 0ms8004kbC++171.2kb2024-07-31 15:36:582024-07-31 15:36:59

Judging History

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

  • [2024-07-31 15:36:59]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:8004kb
  • [2024-07-31 15:36:58]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10; 
int n;
int cnt;
int vis[N];
int vis1[N];
struct edge{
	int to,nex;
	char w;
}e[N];
int head[N];
bool flag[27];
void add_edge(int x,int y,char c)
{
	cnt++;
	e[cnt].to = y;
	e[cnt].w = c;
	e[cnt].nex = head[x];
	head[x] = cnt;
}
int bfs(int x)
{
	memset(vis1,0,sizeof(vis1));
	queue<int>q;
	vis1[x] = 1;
	q.push(x);
	while(!q.empty())
	{
		int tem = q.front();
		q.pop();
		memset(flag,0,sizeof(flag));
	for(int i = head[tem] ; i ; i = e[i].nex)
	{
		int y = e[i].to;
		if(vis[y]) return 0;
		if(flag[e[i].w-'a'+1]) return 0;
		flag[e[i].w-'a'+1]=1;
	}
	}
	return 1;
}

void slove()
{
	memset(vis,0,sizeof(vis));
	cnt = 0;
	int ans = 0;
	cin>>n;
	for(int i = 1 ; i < n ; i++ )
	{
		int x,y;
		char c;
		cin>>x>>y>>c;
		add_edge(x,y,c);
		add_edge(y,x,c);
	}
	for(int i = 1 ; i <= n ; i++ )
	{
		if(bfs(i)==0) vis[i] = 1;
		if(bfs(i)) 
		{
//			cout<<i<<" ";
			ans++;
		}
	}
//	for(int i = 1 ; i <= n ; i++ )
//	cout<<head[i]<<" ";
	cout<<ans<<endl;
}
int main()
{
	int tcase;
	cin>>tcase;
	while(tcase--)
	{
		slove();
	}
	return 0;
}
/*
2
6
3 1 a
3 2 a
3 4 b
4 5 c
4 6 d
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
6
3 1 a
3 2 a
3 4 b
4 5 c
4 6 d
6
3 1 a
3 2 a
3 4 b
5 4 c
6 4 c

output:

2
0

result:

ok 2 number(s): "2 0"

Test #2:

score: -100
Time Limit Exceeded

input:

1112
19
15 18 a
7 18 c
11 14 b
10 17 b
8 14 a
1 3 b
12 2 a
16 3 c
16 4 b
2 3 a
15 5 a
3 18 d
16 9 a
18 13 b
8 4 b
17 7 a
9 6 a
13 19 a
52
8 32 a
14 51 a
30 52 a
48 36 b
27 39 c
39 51 b
35 15 a
51 52 d
45 51 e
39 26 a
20 12 b
34 18 a
9 12 e
25 5 a
9 13 b
41 51 c
1 52 n
33 14 c
22 30 b
17 4 b
12 52 c
...

output:

9
13
0
8
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0...

result: