This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5+7;
vector<int>g[MAXN];
vector<pair<int,int> >bus;
int sajz[MAXN],res[MAXN],t[MAXN];
map<pair<int,int>, bool> w;
int ans,cnt;
void DFS(int x,int nr){
t[x]=nr;
//cout<<x<<' ';
sajz[nr]++;
for(auto s:g[x]) if(!t[s]) DFS(s,nr);
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,m,s1,s2,a,b; char c;
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>a>>b>>c;
if(c=='T'){
g[a].push_back(b); g[b].push_back(a);
}
else bus.push_back({a,b});
}
for(int i=1;i<=n;i++){
if(t[i]==0){
//cout<<"\ni="<<i<<'\n';
DFS(i,++cnt);
}
}
for(auto k:bus){
s1=t[k.first]; s2=t[k.second];
if(s1!=s2 and w[{min(s1,s2),max(s1,s2)}]==0){
res[s1]++; res[s2]++;
w[{min(s1,s2),max(s1,s2)}]=1;
}
}
for(int i=1;i<=cnt;i++){
if(res[i]==cnt-1){
//cout<<i<<' '<<sajz[i]<<'\n';
ans+=sajz[i];
}
}
cout<<ans<<'\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |