Submission #642829

# Submission time Handle Problem Language Result Execution time Memory
642829 2022-09-20T15:28:03 Z JvThunder Monthly railway pass (LMIO18_menesinis_bilietas) C++14
100 / 100
366 ms 27372 KB
// JvThunder ORZ Template
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
typedef long long ll;

using namespace std;

const int N = 5e5+5;
int n,m;
int par[N];
int sz[N];
int deg[N];

vector<pii> train;
vector<pii> bus;

int find(int x)
{
    if(par[x]==x) return x;
    return par[x] = find(par[x]);
}
void join(int a,int b)
{
    int ra = find(a); int rb = find(b);
    if(ra==rb) return;
    par[rb] = ra;
    sz[ra] += sz[rb];
    sz[rb] = 0; 
}

void solve()
{
	cin >> n >> m;
    for(int i=0;i<m;i++)
    {
        int a,b;
        char type;
        cin >> a >> b >> type;
        if(type=='T') train.pb({a,b});
        else bus.pb({a,b}); 
    }

    for(int i=1;i<=n;i++) par[i] = i, sz[i] = 1;
    for(auto x:train) join(x.F,x.S);

    int cc = 0;
    for(int i=1;i<=n;i++) if(par[i]==i) cc++;

    set<pii> s;
    for(auto x:bus)
    {
        int a = find(x.F); int b = find(x.S);
        if(a==b) continue;
        s.insert({min(a,b),max(a,b)});
    }

    for(auto x:s)
    {
        int a = x.F; int b = x.S;
        deg[a]++; deg[b]++;
    }

    int ans = 0;
    for(int i=1;i<=n;i++)
    {
        if(i==par[i])
        {
            if(deg[i]==cc-1) ans += sz[i];
        }
    }
    cout << ans << endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	solve();
 	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 105 ms 6316 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 4 ms 3924 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 32 ms 2412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3924 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 3 ms 724 KB Output is correct
6 Correct 287 ms 18848 KB Output is correct
7 Correct 366 ms 27372 KB Output is correct
8 Correct 7 ms 1236 KB Output is correct
9 Correct 13 ms 1964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 3 ms 724 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 3 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 3 ms 468 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 3 ms 724 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 7 ms 1236 KB Output is correct
23 Correct 13 ms 1964 KB Output is correct
24 Correct 32 ms 2412 KB Output is correct
25 Correct 7 ms 740 KB Output is correct
26 Correct 100 ms 4828 KB Output is correct
27 Correct 39 ms 2412 KB Output is correct
28 Correct 94 ms 7612 KB Output is correct
29 Correct 30 ms 1520 KB Output is correct
30 Correct 69 ms 6616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 7 ms 740 KB Output is correct
17 Correct 100 ms 4828 KB Output is correct
18 Correct 39 ms 2412 KB Output is correct
19 Correct 94 ms 7612 KB Output is correct
20 Correct 30 ms 1520 KB Output is correct
21 Correct 69 ms 6616 KB Output is correct
22 Correct 105 ms 6316 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 4 ms 3924 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 3 ms 724 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 287 ms 18848 KB Output is correct
31 Correct 366 ms 27372 KB Output is correct
32 Correct 7 ms 1236 KB Output is correct
33 Correct 13 ms 1964 KB Output is correct
34 Correct 32 ms 2412 KB Output is correct
35 Correct 19 ms 1928 KB Output is correct
36 Correct 149 ms 7092 KB Output is correct
37 Correct 42 ms 4292 KB Output is correct