QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#387344#6729. Unrooted TrieMayuriWA 188ms17380kbC++201.4kb2024-04-12 13:49:362024-04-12 13:49:37

Judging History

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

  • [2024-04-12 13:49:37]
  • 评测
  • 测评结果:WA
  • 用时:188ms
  • 内存:17380kb
  • [2024-04-12 13:49:36]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define endl '\n'
const ll mod = 1e9 + 7;
const ll maxn = 2e5 + 5;
ll T;
ll n;
ll ecnt = 0;
ll head[maxn];
ll to[maxn << 1];
ll nxt[maxn << 1];
char w[maxn << 1];
bool vis[maxn << 1];
bool dp[maxn << 1];
void add(ll u, ll v, char ch) {
	++ecnt;
	w[ecnt] = ch;
	nxt[ecnt] = head[u];
	to[ecnt] = v;
	head[u] = ecnt;
}
void clear() {
	for (ll i = 1; i <= n; i++)head[i] = 0;
	for (ll i = 1; i <= ecnt; i++)vis[i] = false;
	ecnt = 0;
}
bool dfs(ll u, ll fa) {
	bool flag = true;
	char ch = '#';
	for (ll i = head[u]; i; i = nxt[i]) {
		ll v = to[i];
		if (fa == v)continue;
		if (ch == w[i])flag = false;
		ch = w[i];
		if (!vis[i]) {
			vis[i] = true;
			dp[i] &= dfs(v, u);
		}
		flag &= dp[i];
	}
	return flag;
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	ll T; cin >> T;
	while (T--) {
		clear();
		cin >> n;
		for (ll i = 1; i <= n - 1; i++) {
			ll u, v; char ch;
			cin >> u >> v >> ch;
			add(u, v, ch);
			add(v, u, ch);
		}
        for(ll i = 1; i <= ecnt; i++) dp[i] = true;
		ll ans = 0;
		for (ll i = 1; i <= n; i++) if (dfs(i, 0))ans++;
        // for(ll i = 1; i <= n;i++){
        //     for(ll j = head[i]; j; j = nxt[j]){
        //         cout << i << " " << to[j] << " " << dp[j] << endl;
        //     }
        // }
		cout << ans << endl;
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Wrong Answer
time: 188ms
memory: 17380kb

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
23
72
34
17
28
30
3
4
34
70
41
42
19
26
58
15
31
21
11
73
73
66
35
45
37
82
51
40
78
49
22
15
41
10
37
48
52
5
16
62
8
42
15
85
23
75
26
38
37
65
66
58
32
68
17
8
41
60
88
40
56
17
42
70
12
22
2
62
22
54
14
91
88
1
80
11
51
70
83
33
87
46
56
63
74
81
12
4
59
20
56
19
9
4
40
39
89
35
68
46
33
7...

result:

wrong answer 3rd numbers differ - expected: '22', found: '23'