QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#137685#6729. Unrooted TriePetroTarnavskyi#AC ✓230ms36836kbC++171.8kb2023-08-10 16:37:142023-08-10 16:37:15

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-10 16:37:15]
  • 评测
  • 测评结果:AC
  • 用时:230ms
  • 内存:36836kb
  • [2023-08-10 16:37:14]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define FOR(i, a, b) for (int i = (a); i<(b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i>=(a); --i)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define FILL(a, b) memset(a, b, sizeof(a))

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;

const int N = 1 << 17;
vector<PII> g[N];
int val[N];
int k = 0;
bool ok = 1;

void dfs1(int v, int par){
	map<int, int> uniq;
	PII para = MP(-1, -1);
	
	for(auto e : g[v]){
		if(uniq.count(e.S))
			para = MP(uniq[e.S], e.F);
		
		uniq[e.S] = e.F;
	}
		
	if(SZ(g[v]) - SZ(uniq) > 1){
		ok = 0;
		return;
	}
	for(auto e : g[v])
		if(e.F != par)
			dfs1(e.F, v);
	if(para.F == -1)
		return;
		
	if(para.F == par || para.S == par){
		int to = (para.F == par) ? para.S : para.F;
		val[0]++;
		val[v]--;
		val[to]++;
		k++;
	}
	else{
		val[para.F]++;
		val[para.S]++;
		k++;
	}
}
int ans = 0;
void dfs2(int v, int par, int va){
	va += val[v];
	if(va == k)
		ans++;
		
	//cout << v << " " <<  va << endl;
	
	for(auto e : g[v])
		if(e.F != par)
			dfs2(e.F, v, va);
	
	va -= val[v];
}

void clear(int n){
	FOR(i, 0, n){
		g[i].clear();
		val[i] = 0;
	}
	k = 0;
	ok = 1;
	ans = 0;
}

void solve(){
	int n;
	cin >> n;
	FOR(i, 0, n - 1){
		int a, b;
		char x;
		cin >> a >> b >> x;
		a--; b--;
		g[a].PB(MP(b, x - 'a'));
		g[b].PB(MP(a, x - 'a'));
	}
	dfs1(0, 0);
	if(ok == 0){
		cout << "0\n";
		clear(n);
		return;
	}
	
	dfs2(0, 0, 0);
	cout << ans << "\n";
	clear(n);
}


int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int t;
	cin >> t;
	while(t--)
		solve();
	
	
	return 0;
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

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

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: 0
Accepted
time: 230ms
memory: 36836kb

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:

15
49
22
68
34
17
28
27
3
4
34
70
37
39
19
24
58
8
16
14
10
73
73
65
35
45
33
81
46
35
78
49
22
13
26
10
33
48
47
3
9
50
8
37
15
84
23
75
26
35
35
61
65
58
30
56
11
8
39
60
88
40
56
17
42
62
12
11
2
59
22
54
14
91
87
1
80
11
45
69
80
33
87
46
56
62
54
80
7
4
48
20
55
19
9
4
38
39
89
35
63
46
24
7
52...

result:

ok 1112 numbers