Submission #382152

# Submission time Handle Problem Language Result Execution time Memory
382152 2021-03-26T15:27:20 Z mohamedsobhi777 Monthly railway pass (LMIO18_menesinis_bilietas) C++14
100 / 100
284 ms 40052 KB
#include <bits/stdc++.h>

using namespace std;

#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define loop(_) for (int __ = 0; __ < (_); ++__)
#define pb push_back
#define f first
#define s second
#define sz(_) ((int)_.size())
#define all(_) _.begin(), _.end()
#define lb lower_bound
#define ub upper_bound

using ll = long long;
using ld = long double;

const int N = 5e5 + 7;
const ll mod = 1e9 + 7;

int n, m;
vii e;
vi adj[N];

struct dsu
{
       int fat[N];
       int cnt[N] ;
       int comp = 0;
       dsu()
       {
              fill(cnt , cnt + N , 1) ;
              iota(fat, fat + N, 0);
       }
       void link(int u, int v)
       {
              u = find(u);
              v = find(v);
              if(u != v){
                     ++ comp ; 
                     cnt[v] += cnt[u] ; 
                     cnt[u] = 0 ; 
                     fat[u] = v; 
              }
       }

       int find(int x)
       {
              return fat[x] = x == fat[x] ? x : find(fat[x]);
       }

} d1, d2;

int main()
{
       ios_base::sync_with_stdio(0);
       cin.tie(0);
#ifndef ONLINE_JUDGE
#endif
       cin >> n >> m;
       for (int i = 0; i < m; ++i)
       {
              int u, v;
              char c;
              cin >> u >> v >> c;
              if (c == 'T')
              {
                     d1.link(u, v);
              }
              else
              {
                     e.pb({u, v});
              }
              d2.link(u, v);
       }

       if (d2.comp != n - 1)
              return cout << 0, 0;

       int ans = 0;  


       if (e.empty())
       {
              return cout << n, 0;
       }
       else
       {
              for (auto u : e)
              {
                     int v1 = d1.find(u.f);
                     int v2 = d1.find(u.s);
                     if (v1 != v2)
                     {
                            adj[v1].pb(v2);
                            adj[v2].pb(v1);
                     }
              }
              for(int i =1 ;i <= n;++ i){
                     sort(all(adj[i])) ; 
                     adj[i].erase(unique(all(adj[i])) , adj[i].end()) ;
              }
              int ac = n - d1.comp ;

              for(int i = 1;i <= n;++ i){
                     if(sz(adj[i]) == ac - 1){
                            ans += d1.cnt[ i ] ;
                     }
              }
              cout << ans ;
       }

       return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 208 ms 20076 KB Output is correct
2 Correct 13 ms 19948 KB Output is correct
3 Correct 13 ms 19948 KB Output is correct
4 Correct 13 ms 19948 KB Output is correct
5 Correct 13 ms 19948 KB Output is correct
6 Correct 56 ms 19948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19948 KB Output is correct
2 Correct 13 ms 19948 KB Output is correct
3 Correct 13 ms 19948 KB Output is correct
4 Correct 13 ms 19948 KB Output is correct
5 Correct 15 ms 20076 KB Output is correct
6 Correct 154 ms 26588 KB Output is correct
7 Correct 284 ms 40052 KB Output is correct
8 Correct 19 ms 20588 KB Output is correct
9 Correct 25 ms 20720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19948 KB Output is correct
2 Correct 13 ms 19948 KB Output is correct
3 Correct 13 ms 19948 KB Output is correct
4 Correct 13 ms 19948 KB Output is correct
5 Correct 15 ms 20076 KB Output is correct
6 Correct 13 ms 19948 KB Output is correct
7 Correct 16 ms 19948 KB Output is correct
8 Correct 14 ms 19948 KB Output is correct
9 Correct 15 ms 20076 KB Output is correct
10 Correct 15 ms 20076 KB Output is correct
11 Correct 15 ms 20076 KB Output is correct
12 Correct 13 ms 19948 KB Output is correct
13 Correct 13 ms 19948 KB Output is correct
14 Correct 16 ms 20204 KB Output is correct
15 Correct 13 ms 19948 KB Output is correct
16 Correct 14 ms 19948 KB Output is correct
17 Correct 13 ms 19948 KB Output is correct
18 Correct 13 ms 19948 KB Output is correct
19 Correct 13 ms 19948 KB Output is correct
20 Correct 13 ms 19948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19948 KB Output is correct
2 Correct 16 ms 19948 KB Output is correct
3 Correct 14 ms 19948 KB Output is correct
4 Correct 15 ms 20076 KB Output is correct
5 Correct 15 ms 20076 KB Output is correct
6 Correct 15 ms 20076 KB Output is correct
7 Correct 13 ms 19948 KB Output is correct
8 Correct 13 ms 19948 KB Output is correct
9 Correct 16 ms 20204 KB Output is correct
10 Correct 13 ms 19948 KB Output is correct
11 Correct 14 ms 19948 KB Output is correct
12 Correct 13 ms 19948 KB Output is correct
13 Correct 13 ms 19948 KB Output is correct
14 Correct 13 ms 19948 KB Output is correct
15 Correct 13 ms 19948 KB Output is correct
16 Correct 13 ms 19948 KB Output is correct
17 Correct 13 ms 19948 KB Output is correct
18 Correct 13 ms 19948 KB Output is correct
19 Correct 13 ms 19948 KB Output is correct
20 Correct 15 ms 20076 KB Output is correct
21 Correct 13 ms 19948 KB Output is correct
22 Correct 19 ms 20588 KB Output is correct
23 Correct 25 ms 20720 KB Output is correct
24 Correct 56 ms 19948 KB Output is correct
25 Correct 22 ms 20464 KB Output is correct
26 Correct 135 ms 26460 KB Output is correct
27 Correct 58 ms 23012 KB Output is correct
28 Correct 77 ms 24036 KB Output is correct
29 Correct 47 ms 22248 KB Output is correct
30 Correct 70 ms 22500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 19948 KB Output is correct
2 Correct 16 ms 19948 KB Output is correct
3 Correct 14 ms 19948 KB Output is correct
4 Correct 15 ms 20076 KB Output is correct
5 Correct 15 ms 20076 KB Output is correct
6 Correct 15 ms 20076 KB Output is correct
7 Correct 13 ms 19948 KB Output is correct
8 Correct 13 ms 19948 KB Output is correct
9 Correct 16 ms 20204 KB Output is correct
10 Correct 13 ms 19948 KB Output is correct
11 Correct 14 ms 19948 KB Output is correct
12 Correct 13 ms 19948 KB Output is correct
13 Correct 13 ms 19948 KB Output is correct
14 Correct 13 ms 19948 KB Output is correct
15 Correct 13 ms 19948 KB Output is correct
16 Correct 22 ms 20464 KB Output is correct
17 Correct 135 ms 26460 KB Output is correct
18 Correct 58 ms 23012 KB Output is correct
19 Correct 77 ms 24036 KB Output is correct
20 Correct 47 ms 22248 KB Output is correct
21 Correct 70 ms 22500 KB Output is correct
22 Correct 208 ms 20076 KB Output is correct
23 Correct 13 ms 19948 KB Output is correct
24 Correct 13 ms 19948 KB Output is correct
25 Correct 13 ms 19948 KB Output is correct
26 Correct 13 ms 19948 KB Output is correct
27 Correct 13 ms 19948 KB Output is correct
28 Correct 15 ms 20076 KB Output is correct
29 Correct 13 ms 19948 KB Output is correct
30 Correct 154 ms 26588 KB Output is correct
31 Correct 284 ms 40052 KB Output is correct
32 Correct 19 ms 20588 KB Output is correct
33 Correct 25 ms 20720 KB Output is correct
34 Correct 56 ms 19948 KB Output is correct
35 Correct 36 ms 21484 KB Output is correct
36 Correct 197 ms 32604 KB Output is correct
37 Correct 76 ms 23020 KB Output is correct