QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#259967#6729. Unrooted TrieAH20AC ✓344ms39736kbC++142.4kb2023-11-21 17:02:282023-11-21 17:02:28

Judging History

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

  • [2023-11-21 17:02:28]
  • 评测
  • 测评结果:AC
  • 用时:344ms
  • 内存:39736kb
  • [2023-11-21 17:02:28]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;
const int maxm = 1e5+7;
int cnt[maxm][30],cur[maxm][30],dfn[maxm],rk[maxm],R[maxm],ct = 0;
int dif[maxm],sum[maxm];
vector<int>edge[maxm];
vector<array<int,3>>all;
//所有的产生重复的三元组,按照[u,x,y]的顺序排布
void init(int n){
    all.clear();ct = 0;
    for(int i=1;i<=n+3;i++){
        edge[i].clear();
        dif[i] = sum[i] = 0;
        for(int j=0;j<26;j++){
            cnt[i][j] = cur[i][j] = 0;
        }
    }
}
void dfs(int now,int fa){
    dfn[++ct] = now,rk[now] = ct;
    for(auto u:edge[now]){
        if(u == fa) continue;
        dfs(u,now);
    }
    R[now] = ct;
}
void solve(){
    int n;cin>>n;
    init(n);
    for(int i=1;i<n;i++){
        int u,v;char ch;
        cin>>u>>v>>ch;
        cnt[u][ch - 'a']++;
        cnt[v][ch - 'a']++;
        if(cnt[u][ch - 'a'] == 2){
            all.push_back({u,v,cur[u][ch - 'a']});
        }
        if(cnt[v][ch - 'a'] == 2){
            all.push_back({v,u,cur[v][ch - 'a']});
        }
        cur[u][ch - 'a'] = v;
        cur[v][ch - 'a'] = u;
        edge[u].push_back(v);
        edge[v].push_back(u);
        //建树完成
        //我们要得到[u,x,y]这个三元组
    }
    for(int i=1;i<=n;i++){
        //判断是否存在合法解
        int tot = 0;
        for(int j=0;j<26;j++){
            if(cnt[i][j] >= 3){
                cout<<"0\n";
                return;
            }
            else if(cnt[i][j] == 2) ++tot;
        }
        if(tot > 1){
            cout<<"0\n";
            return;
        }
    }
    dfs(1,0);//得到dfs序
    for(auto item:all){
        int u = item[0],x = item[1],y = item[2];
        if(rk[u] < rk[x] and rk[u] < rk[y]){
            //u在最前面 ,这时根节点只能是x或者y的子树
            dif[rk[x]]++;dif[R[x] + 1]--;
            dif[rk[y]]++;dif[R[y] + 1]--;
        }
        else{
            if(rk[x]>rk[y]) swap(x,y);
            //全局+1,u的子树-1,y的子树+1
            dif[1]++;dif[n+1]--;
            dif[rk[u]]--;dif[R[u] + 1]++;
            dif[rk[y]]++;dif[R[y] + 1]--;
        }
    }
    int sz = all.size(),tot = 0;
    for(int i=1;i<=n;i++){
        sum[i] = sum[i-1] + dif[i];
        if(sz == sum[i]){ ++tot;}
    }
    cout<<tot<<'\n';
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int T;cin>>T;
    while(T--) solve();
    return 0;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 344ms
memory: 39736kb

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