Submission #942122

#TimeUsernameProblemLanguageResultExecution timeMemory
942122MathiasMonthly railway pass (LMIO18_menesinis_bilietas)C++14
100 / 100
347 ms52672 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...