#include <bits/stdc++.h>
//#define int long long
#define ld long double
#define endl "\n"
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define Unique(v) sort(all(v));v.erase(unique(all(v)),v.end());
#define sz(v) ((int)v.size())
using namespace std;
typedef vector<int> vi;
#define y1 abacaba
//#define left oooooopss
#define db(x) cerr << #x <<" == "<<x << endl;
#define db2(x,y) cerr<<#x <<" == "<<x<<", "<<#y<<" == "<<y<<endl;
#define db3(x,y,z) cerr << #x<<" == "<<x<<", "<<#y<<" == "<<y<<", "<<#z<<" == "<<z<<endl;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;
std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
//inline ll mod(ll n, ll m ){ ll ret = n%m; if(ret < 0) ret += m; return ret; }
ll gcd(ll a, ll b){return (b == 0LL ? a : gcd(b, a%b));}
ll exp(ll b,ll e,ll m){
b%=m;
ll ans = 1;
for (; e; b = b * b % m, e /= 2)
if (e & 1) ans = ans * b % m;
return ans;
}
// debug:
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T){
cerr << ' ' << H;
dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr<<"(" << #__VA_ARGS__<<"):" , dbg_out(__VA_ARGS__) , cerr << endl
#else
#define dbg(...) 42
#endif
//
const int N = 500100;
vi g[N];
int p[N],sz[N];
int f(int x){return p[x] = (p[x]==x?x:f(p[x]));}
int comps=0;
void join(int a,int b){
a=f(a),b=f(b);
if(a==b)return;
if(sz[a] > sz[b])swap(a,b);
p[a]=b;
sz[b]+=sz[a];
comps--;
}
set<int> here[N];
int32_t main(){
fastio;
int n,m;
cin >> n >> m;
comps=n;
rep(i,1,n+1)p[i]=i,sz[i]=1;
rep(i,0,m){
int a,b;
char c;
cin >> a >> b >> c;
if(c == 'T')join(a,b);
else{
g[a].pb(b);
g[b].pb(a);
}
}
rep(i,1,n+1){
here[f(i)].insert(f(i));
for(auto to : g[i]){
here[f(i)].insert(f(to));
}
}
int res=0;
rep(i,1,n+1)res+=(sz(here[f(i)]) == comps);
cout << res << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
44644 KB |
Output is correct |
2 |
Correct |
17 ms |
35564 KB |
Output is correct |
3 |
Correct |
16 ms |
35512 KB |
Output is correct |
4 |
Correct |
45 ms |
61268 KB |
Output is correct |
5 |
Correct |
18 ms |
36268 KB |
Output is correct |
6 |
Correct |
47 ms |
37904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
61268 KB |
Output is correct |
2 |
Correct |
18 ms |
36268 KB |
Output is correct |
3 |
Correct |
18 ms |
35460 KB |
Output is correct |
4 |
Correct |
18 ms |
35624 KB |
Output is correct |
5 |
Correct |
19 ms |
36348 KB |
Output is correct |
6 |
Correct |
206 ms |
77316 KB |
Output is correct |
7 |
Correct |
534 ms |
116416 KB |
Output is correct |
8 |
Correct |
27 ms |
38484 KB |
Output is correct |
9 |
Correct |
33 ms |
39960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
35564 KB |
Output is correct |
2 |
Correct |
16 ms |
35512 KB |
Output is correct |
3 |
Correct |
18 ms |
35460 KB |
Output is correct |
4 |
Correct |
18 ms |
35624 KB |
Output is correct |
5 |
Correct |
19 ms |
36348 KB |
Output is correct |
6 |
Correct |
17 ms |
35504 KB |
Output is correct |
7 |
Correct |
17 ms |
35536 KB |
Output is correct |
8 |
Correct |
19 ms |
35656 KB |
Output is correct |
9 |
Correct |
19 ms |
35712 KB |
Output is correct |
10 |
Correct |
19 ms |
35836 KB |
Output is correct |
11 |
Correct |
20 ms |
35868 KB |
Output is correct |
12 |
Correct |
19 ms |
35616 KB |
Output is correct |
13 |
Correct |
18 ms |
35540 KB |
Output is correct |
14 |
Correct |
21 ms |
35964 KB |
Output is correct |
15 |
Correct |
17 ms |
35568 KB |
Output is correct |
16 |
Correct |
18 ms |
35556 KB |
Output is correct |
17 |
Correct |
18 ms |
35540 KB |
Output is correct |
18 |
Correct |
18 ms |
35668 KB |
Output is correct |
19 |
Correct |
18 ms |
35628 KB |
Output is correct |
20 |
Correct |
17 ms |
35716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
35504 KB |
Output is correct |
2 |
Correct |
17 ms |
35536 KB |
Output is correct |
3 |
Correct |
19 ms |
35656 KB |
Output is correct |
4 |
Correct |
19 ms |
35712 KB |
Output is correct |
5 |
Correct |
19 ms |
35836 KB |
Output is correct |
6 |
Correct |
20 ms |
35868 KB |
Output is correct |
7 |
Correct |
19 ms |
35616 KB |
Output is correct |
8 |
Correct |
18 ms |
35540 KB |
Output is correct |
9 |
Correct |
21 ms |
35964 KB |
Output is correct |
10 |
Correct |
17 ms |
35568 KB |
Output is correct |
11 |
Correct |
18 ms |
35556 KB |
Output is correct |
12 |
Correct |
18 ms |
35540 KB |
Output is correct |
13 |
Correct |
18 ms |
35668 KB |
Output is correct |
14 |
Correct |
18 ms |
35628 KB |
Output is correct |
15 |
Correct |
17 ms |
35716 KB |
Output is correct |
16 |
Correct |
17 ms |
35564 KB |
Output is correct |
17 |
Correct |
16 ms |
35512 KB |
Output is correct |
18 |
Correct |
18 ms |
35460 KB |
Output is correct |
19 |
Correct |
18 ms |
35624 KB |
Output is correct |
20 |
Correct |
19 ms |
36348 KB |
Output is correct |
21 |
Correct |
18 ms |
36268 KB |
Output is correct |
22 |
Correct |
27 ms |
38484 KB |
Output is correct |
23 |
Correct |
33 ms |
39960 KB |
Output is correct |
24 |
Correct |
47 ms |
37904 KB |
Output is correct |
25 |
Correct |
25 ms |
36300 KB |
Output is correct |
26 |
Correct |
142 ms |
45108 KB |
Output is correct |
27 |
Correct |
54 ms |
39088 KB |
Output is correct |
28 |
Correct |
94 ms |
51088 KB |
Output is correct |
29 |
Correct |
45 ms |
38560 KB |
Output is correct |
30 |
Correct |
108 ms |
49248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
35504 KB |
Output is correct |
2 |
Correct |
17 ms |
35536 KB |
Output is correct |
3 |
Correct |
19 ms |
35656 KB |
Output is correct |
4 |
Correct |
19 ms |
35712 KB |
Output is correct |
5 |
Correct |
19 ms |
35836 KB |
Output is correct |
6 |
Correct |
20 ms |
35868 KB |
Output is correct |
7 |
Correct |
19 ms |
35616 KB |
Output is correct |
8 |
Correct |
18 ms |
35540 KB |
Output is correct |
9 |
Correct |
21 ms |
35964 KB |
Output is correct |
10 |
Correct |
17 ms |
35568 KB |
Output is correct |
11 |
Correct |
18 ms |
35556 KB |
Output is correct |
12 |
Correct |
18 ms |
35540 KB |
Output is correct |
13 |
Correct |
18 ms |
35668 KB |
Output is correct |
14 |
Correct |
18 ms |
35628 KB |
Output is correct |
15 |
Correct |
17 ms |
35716 KB |
Output is correct |
16 |
Correct |
25 ms |
36300 KB |
Output is correct |
17 |
Correct |
142 ms |
45108 KB |
Output is correct |
18 |
Correct |
54 ms |
39088 KB |
Output is correct |
19 |
Correct |
94 ms |
51088 KB |
Output is correct |
20 |
Correct |
45 ms |
38560 KB |
Output is correct |
21 |
Correct |
108 ms |
49248 KB |
Output is correct |
22 |
Correct |
125 ms |
44644 KB |
Output is correct |
23 |
Correct |
17 ms |
35564 KB |
Output is correct |
24 |
Correct |
16 ms |
35512 KB |
Output is correct |
25 |
Correct |
45 ms |
61268 KB |
Output is correct |
26 |
Correct |
18 ms |
35460 KB |
Output is correct |
27 |
Correct |
18 ms |
35624 KB |
Output is correct |
28 |
Correct |
19 ms |
36348 KB |
Output is correct |
29 |
Correct |
18 ms |
36268 KB |
Output is correct |
30 |
Correct |
206 ms |
77316 KB |
Output is correct |
31 |
Correct |
534 ms |
116416 KB |
Output is correct |
32 |
Correct |
27 ms |
38484 KB |
Output is correct |
33 |
Correct |
33 ms |
39960 KB |
Output is correct |
34 |
Correct |
47 ms |
37904 KB |
Output is correct |
35 |
Correct |
45 ms |
38988 KB |
Output is correct |
36 |
Correct |
339 ms |
52596 KB |
Output is correct |
37 |
Correct |
74 ms |
40276 KB |
Output is correct |