#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5*5+1;
int n, m;
vector<pair<bool, int>> adj[maxN];
int par[maxN];
int s[maxN];
vector<int> routes[maxN];
bool checkedContainer[maxN];
int find(int a){
if(par[a]==a)return a;
return par[a]=find(par[a]);
}
void unite(int a, int b){
a = find(a), b = find(b);
if(a==b)return;
par[b]=a;
s[a]+=s[b];
}
bool same(int a, int b){
return find(a)==find(b);
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(nullptr);
cin>>n>>m;
for(int i = 0; i < m; i++){
int a, b;
char t;
bool type;
cin>>a>>b>>t;
if(t=='A')type=0;
else type=1;
adj[a].push_back({type, b});
adj[b].push_back({type, a});
}
for(int i = 0; i < maxN; i++)par[i]=i;
for(int i = 0; i < maxN; i++)s[i]=1;
for(int i = 1; i <= n; i++){
for(auto x : adj[i]){
if(x.first==0){
}else{
unite(x.second, i);
}
}
}
for(int i = 1; i <= n; i++){
for(auto x : adj[i]){
if(x.first==0){
routes[find(i)].push_back(find(x.second));
routes[find(x.second)].push_back(find(i));
}else{
}
}
}
int ans=0;
bool checked[maxN]={0};
for(int i = 1; i <= n; i++){
if(checkedContainer[find(i)])continue;
int ss=s[find(i)];
vector<int> used;
for(auto x : routes[find(i)]){
if(!same(i, x)&&!checked[find(x)]){
ss+=s[find(x)];
checked[find(x)]=1;
used.push_back(find(x));
}
}
for(auto x : used){
checked[x]=0;
}
if(ss==n){
ans+=s[find(i)];
checkedContainer[find(i)]=true;
}
}
cout<<ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
362 ms |
43172 KB |
Output is correct |
2 |
Correct |
17 ms |
28260 KB |
Output is correct |
3 |
Correct |
18 ms |
28236 KB |
Output is correct |
4 |
Correct |
24 ms |
28112 KB |
Output is correct |
5 |
Correct |
16 ms |
28196 KB |
Output is correct |
6 |
Correct |
67 ms |
32856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
28112 KB |
Output is correct |
2 |
Correct |
16 ms |
28196 KB |
Output is correct |
3 |
Correct |
15 ms |
28200 KB |
Output is correct |
4 |
Correct |
16 ms |
28244 KB |
Output is correct |
5 |
Correct |
19 ms |
28492 KB |
Output is correct |
6 |
Correct |
150 ms |
42204 KB |
Output is correct |
7 |
Correct |
429 ms |
63716 KB |
Output is correct |
8 |
Correct |
24 ms |
29516 KB |
Output is correct |
9 |
Correct |
31 ms |
29956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
28260 KB |
Output is correct |
2 |
Correct |
18 ms |
28236 KB |
Output is correct |
3 |
Correct |
15 ms |
28200 KB |
Output is correct |
4 |
Correct |
16 ms |
28244 KB |
Output is correct |
5 |
Correct |
19 ms |
28492 KB |
Output is correct |
6 |
Correct |
15 ms |
28096 KB |
Output is correct |
7 |
Correct |
15 ms |
28200 KB |
Output is correct |
8 |
Correct |
17 ms |
28364 KB |
Output is correct |
9 |
Correct |
16 ms |
28452 KB |
Output is correct |
10 |
Correct |
20 ms |
28704 KB |
Output is correct |
11 |
Correct |
18 ms |
28392 KB |
Output is correct |
12 |
Correct |
17 ms |
28108 KB |
Output is correct |
13 |
Correct |
16 ms |
28108 KB |
Output is correct |
14 |
Correct |
19 ms |
28788 KB |
Output is correct |
15 |
Correct |
17 ms |
28192 KB |
Output is correct |
16 |
Correct |
19 ms |
28304 KB |
Output is correct |
17 |
Correct |
19 ms |
28108 KB |
Output is correct |
18 |
Correct |
15 ms |
28236 KB |
Output is correct |
19 |
Correct |
20 ms |
28236 KB |
Output is correct |
20 |
Correct |
17 ms |
28236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
28096 KB |
Output is correct |
2 |
Correct |
15 ms |
28200 KB |
Output is correct |
3 |
Correct |
17 ms |
28364 KB |
Output is correct |
4 |
Correct |
16 ms |
28452 KB |
Output is correct |
5 |
Correct |
20 ms |
28704 KB |
Output is correct |
6 |
Correct |
18 ms |
28392 KB |
Output is correct |
7 |
Correct |
17 ms |
28108 KB |
Output is correct |
8 |
Correct |
16 ms |
28108 KB |
Output is correct |
9 |
Correct |
19 ms |
28788 KB |
Output is correct |
10 |
Correct |
17 ms |
28192 KB |
Output is correct |
11 |
Correct |
19 ms |
28304 KB |
Output is correct |
12 |
Correct |
19 ms |
28108 KB |
Output is correct |
13 |
Correct |
15 ms |
28236 KB |
Output is correct |
14 |
Correct |
20 ms |
28236 KB |
Output is correct |
15 |
Correct |
17 ms |
28236 KB |
Output is correct |
16 |
Correct |
17 ms |
28260 KB |
Output is correct |
17 |
Correct |
18 ms |
28236 KB |
Output is correct |
18 |
Correct |
15 ms |
28200 KB |
Output is correct |
19 |
Correct |
16 ms |
28244 KB |
Output is correct |
20 |
Correct |
19 ms |
28492 KB |
Output is correct |
21 |
Correct |
16 ms |
28196 KB |
Output is correct |
22 |
Correct |
24 ms |
29516 KB |
Output is correct |
23 |
Correct |
31 ms |
29956 KB |
Output is correct |
24 |
Correct |
67 ms |
32856 KB |
Output is correct |
25 |
Correct |
23 ms |
29772 KB |
Output is correct |
26 |
Correct |
281 ms |
45572 KB |
Output is correct |
27 |
Correct |
58 ms |
35304 KB |
Output is correct |
28 |
Correct |
74 ms |
37960 KB |
Output is correct |
29 |
Correct |
42 ms |
33816 KB |
Output is correct |
30 |
Correct |
158 ms |
36444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
28096 KB |
Output is correct |
2 |
Correct |
15 ms |
28200 KB |
Output is correct |
3 |
Correct |
17 ms |
28364 KB |
Output is correct |
4 |
Correct |
16 ms |
28452 KB |
Output is correct |
5 |
Correct |
20 ms |
28704 KB |
Output is correct |
6 |
Correct |
18 ms |
28392 KB |
Output is correct |
7 |
Correct |
17 ms |
28108 KB |
Output is correct |
8 |
Correct |
16 ms |
28108 KB |
Output is correct |
9 |
Correct |
19 ms |
28788 KB |
Output is correct |
10 |
Correct |
17 ms |
28192 KB |
Output is correct |
11 |
Correct |
19 ms |
28304 KB |
Output is correct |
12 |
Correct |
19 ms |
28108 KB |
Output is correct |
13 |
Correct |
15 ms |
28236 KB |
Output is correct |
14 |
Correct |
20 ms |
28236 KB |
Output is correct |
15 |
Correct |
17 ms |
28236 KB |
Output is correct |
16 |
Correct |
23 ms |
29772 KB |
Output is correct |
17 |
Correct |
281 ms |
45572 KB |
Output is correct |
18 |
Correct |
58 ms |
35304 KB |
Output is correct |
19 |
Correct |
74 ms |
37960 KB |
Output is correct |
20 |
Correct |
42 ms |
33816 KB |
Output is correct |
21 |
Correct |
158 ms |
36444 KB |
Output is correct |
22 |
Correct |
362 ms |
43172 KB |
Output is correct |
23 |
Correct |
17 ms |
28260 KB |
Output is correct |
24 |
Correct |
18 ms |
28236 KB |
Output is correct |
25 |
Correct |
24 ms |
28112 KB |
Output is correct |
26 |
Correct |
15 ms |
28200 KB |
Output is correct |
27 |
Correct |
16 ms |
28244 KB |
Output is correct |
28 |
Correct |
19 ms |
28492 KB |
Output is correct |
29 |
Correct |
16 ms |
28196 KB |
Output is correct |
30 |
Correct |
150 ms |
42204 KB |
Output is correct |
31 |
Correct |
429 ms |
63716 KB |
Output is correct |
32 |
Correct |
24 ms |
29516 KB |
Output is correct |
33 |
Correct |
31 ms |
29956 KB |
Output is correct |
34 |
Correct |
67 ms |
32856 KB |
Output is correct |
35 |
Correct |
83 ms |
30988 KB |
Output is correct |
36 |
Correct |
936 ms |
47868 KB |
Output is correct |
37 |
Correct |
215 ms |
37704 KB |
Output is correct |