#include <bits/stdc++.h>
//#define int long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define sc second
#define endl "\n"
#define pii pair<int,int>
using namespace std;
const int MAXN = 5e5+5;
const int mod7 = 1e9+7;
const long long inf = 1e18;
int n,m;
struct grana{int a,b;string c;};
vector<vector<pii>> graf(MAXN);
vector<unordered_set<int>> compressGraf(MAXN);
int whichCMP[MAXN];
bool visited[MAXN];
void dfs(int nod, int cmp)
{
visited[nod] = 1;
whichCMP[nod] = cmp;
for(auto x: graf[nod])
{
if(x.sc == 1)continue;
if(!visited[x.fi])dfs(x.fi,cmp);
}
}
signed main()
{
ios_base::sync_with_stdio(false),cin.tie(0), cout.tie(0);
int tt=1;
//cin >> tt;
while(tt--)
{
cin >> n >> m;
vector<grana> svegrane;
map<pii, string> mapa;
for(int i=0; i<m; i++)
{
int a,b;string c;
cin >> a >> b >> c;
if(a>b)swap(a,b);
svegrane.pb({a,b,c});
if(mapa[mp(a,b)] == "T")continue;
mapa[mp(a,b)] = c;
}
//for(auto x: mapa)cout << x.fi.fi << " " << x.fi.sc << ":" << x.sc << endl;
for(auto x: svegrane)
{
int a = x.a;
int b = x.b;
string c = x.c;
if(c == "A" && mapa[mp(a,b)] == "T")continue;
if(c=="T")
{
graf[a].pb(mp(b,0));
graf[b].pb(mp(a,0));
}
else
{
graf[a].pb(mp(b,1));
graf[b].pb(mp(a,1));
}
//svegrane.pb({a,b,c});
}
int c = 0;
for(int i=1; i<=n; i++)
{
if(!visited[i])
{
c++;
dfs(i,c);
}
}
//for(int i=1; i<=n; i++)cout << i << " " << whichCMP[i] << endl;
for(auto x: svegrane)
{
if(x.c == "A" && mapa[mp(x.a,x.b)] == "T")continue;
if(x.c == "T")continue;
if(whichCMP[x.a] == whichCMP[x.b])continue;
compressGraf[whichCMP[x.a]].insert(whichCMP[x.b]);
compressGraf[whichCMP[x.b]].insert(whichCMP[x.a]);
}
int rez = 0;
for(int i=1; i<=n; i++)
{
if(compressGraf[whichCMP[i]].size() == c-1)rez++;
}
cout << rez << endl;
}
}
/*
7 9
7 5 A
3 7 A
3 1 A
3 4 T
3 5 A
7 1 A
5 6 T
2 7 T
6 5 A
*/
Compilation message
menesinis_bilietas.cpp: In function 'int main()':
menesinis_bilietas.cpp:96:49: warning: comparison of integer expressions of different signedness: 'std::unordered_set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
96 | if(compressGraf[whichCMP[i]].size() == c-1)rez++;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
541 ms |
123576 KB |
Output is correct |
2 |
Correct |
18 ms |
39516 KB |
Output is correct |
3 |
Correct |
18 ms |
39616 KB |
Output is correct |
4 |
Correct |
21 ms |
41876 KB |
Output is correct |
5 |
Correct |
18 ms |
39516 KB |
Output is correct |
6 |
Correct |
190 ms |
70992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
41876 KB |
Output is correct |
2 |
Correct |
18 ms |
39516 KB |
Output is correct |
3 |
Correct |
18 ms |
39516 KB |
Output is correct |
4 |
Correct |
20 ms |
39620 KB |
Output is correct |
5 |
Correct |
25 ms |
41136 KB |
Output is correct |
6 |
Correct |
847 ms |
119920 KB |
Output is correct |
7 |
Correct |
1265 ms |
186660 KB |
Output is correct |
8 |
Correct |
37 ms |
44916 KB |
Output is correct |
9 |
Correct |
60 ms |
47560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
39516 KB |
Output is correct |
2 |
Correct |
18 ms |
39616 KB |
Output is correct |
3 |
Correct |
18 ms |
39516 KB |
Output is correct |
4 |
Correct |
20 ms |
39620 KB |
Output is correct |
5 |
Correct |
25 ms |
41136 KB |
Output is correct |
6 |
Correct |
18 ms |
39516 KB |
Output is correct |
7 |
Correct |
19 ms |
39516 KB |
Output is correct |
8 |
Correct |
20 ms |
40028 KB |
Output is correct |
9 |
Correct |
28 ms |
40376 KB |
Output is correct |
10 |
Correct |
26 ms |
41072 KB |
Output is correct |
11 |
Correct |
22 ms |
40632 KB |
Output is correct |
12 |
Correct |
18 ms |
39676 KB |
Output is correct |
13 |
Correct |
18 ms |
39476 KB |
Output is correct |
14 |
Correct |
29 ms |
41328 KB |
Output is correct |
15 |
Correct |
18 ms |
39516 KB |
Output is correct |
16 |
Correct |
19 ms |
39768 KB |
Output is correct |
17 |
Correct |
19 ms |
39580 KB |
Output is correct |
18 |
Correct |
23 ms |
40028 KB |
Output is correct |
19 |
Correct |
19 ms |
39768 KB |
Output is correct |
20 |
Correct |
21 ms |
40148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
39516 KB |
Output is correct |
2 |
Correct |
19 ms |
39516 KB |
Output is correct |
3 |
Correct |
20 ms |
40028 KB |
Output is correct |
4 |
Correct |
28 ms |
40376 KB |
Output is correct |
5 |
Correct |
26 ms |
41072 KB |
Output is correct |
6 |
Correct |
22 ms |
40632 KB |
Output is correct |
7 |
Correct |
18 ms |
39676 KB |
Output is correct |
8 |
Correct |
18 ms |
39476 KB |
Output is correct |
9 |
Correct |
29 ms |
41328 KB |
Output is correct |
10 |
Correct |
18 ms |
39516 KB |
Output is correct |
11 |
Correct |
19 ms |
39768 KB |
Output is correct |
12 |
Correct |
19 ms |
39580 KB |
Output is correct |
13 |
Correct |
23 ms |
40028 KB |
Output is correct |
14 |
Correct |
19 ms |
39768 KB |
Output is correct |
15 |
Correct |
21 ms |
40148 KB |
Output is correct |
16 |
Correct |
18 ms |
39516 KB |
Output is correct |
17 |
Correct |
18 ms |
39616 KB |
Output is correct |
18 |
Correct |
18 ms |
39516 KB |
Output is correct |
19 |
Correct |
20 ms |
39620 KB |
Output is correct |
20 |
Correct |
25 ms |
41136 KB |
Output is correct |
21 |
Correct |
18 ms |
39516 KB |
Output is correct |
22 |
Correct |
37 ms |
44916 KB |
Output is correct |
23 |
Correct |
60 ms |
47560 KB |
Output is correct |
24 |
Correct |
190 ms |
70992 KB |
Output is correct |
25 |
Correct |
48 ms |
44528 KB |
Output is correct |
26 |
Correct |
738 ms |
99252 KB |
Output is correct |
27 |
Correct |
251 ms |
63436 KB |
Output is correct |
28 |
Correct |
370 ms |
79956 KB |
Output is correct |
29 |
Correct |
160 ms |
57812 KB |
Output is correct |
30 |
Correct |
309 ms |
75736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
39516 KB |
Output is correct |
2 |
Correct |
19 ms |
39516 KB |
Output is correct |
3 |
Correct |
20 ms |
40028 KB |
Output is correct |
4 |
Correct |
28 ms |
40376 KB |
Output is correct |
5 |
Correct |
26 ms |
41072 KB |
Output is correct |
6 |
Correct |
22 ms |
40632 KB |
Output is correct |
7 |
Correct |
18 ms |
39676 KB |
Output is correct |
8 |
Correct |
18 ms |
39476 KB |
Output is correct |
9 |
Correct |
29 ms |
41328 KB |
Output is correct |
10 |
Correct |
18 ms |
39516 KB |
Output is correct |
11 |
Correct |
19 ms |
39768 KB |
Output is correct |
12 |
Correct |
19 ms |
39580 KB |
Output is correct |
13 |
Correct |
23 ms |
40028 KB |
Output is correct |
14 |
Correct |
19 ms |
39768 KB |
Output is correct |
15 |
Correct |
21 ms |
40148 KB |
Output is correct |
16 |
Correct |
48 ms |
44528 KB |
Output is correct |
17 |
Correct |
738 ms |
99252 KB |
Output is correct |
18 |
Correct |
251 ms |
63436 KB |
Output is correct |
19 |
Correct |
370 ms |
79956 KB |
Output is correct |
20 |
Correct |
160 ms |
57812 KB |
Output is correct |
21 |
Correct |
309 ms |
75736 KB |
Output is correct |
22 |
Correct |
541 ms |
123576 KB |
Output is correct |
23 |
Correct |
18 ms |
39516 KB |
Output is correct |
24 |
Correct |
18 ms |
39616 KB |
Output is correct |
25 |
Correct |
21 ms |
41876 KB |
Output is correct |
26 |
Correct |
18 ms |
39516 KB |
Output is correct |
27 |
Correct |
20 ms |
39620 KB |
Output is correct |
28 |
Correct |
25 ms |
41136 KB |
Output is correct |
29 |
Correct |
18 ms |
39516 KB |
Output is correct |
30 |
Correct |
847 ms |
119920 KB |
Output is correct |
31 |
Correct |
1265 ms |
186660 KB |
Output is correct |
32 |
Correct |
37 ms |
44916 KB |
Output is correct |
33 |
Correct |
60 ms |
47560 KB |
Output is correct |
34 |
Correct |
190 ms |
70992 KB |
Output is correct |
35 |
Correct |
113 ms |
51168 KB |
Output is correct |
36 |
Correct |
911 ms |
112556 KB |
Output is correct |
37 |
Correct |
232 ms |
74120 KB |
Output is correct |