답안 #642786

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
642786 2022-09-20T13:31:12 Z sanstzu Monthly railway pass (LMIO18_menesinis_bilietas) C++14
0 / 100
360 ms 134300 KB
#include <bits/stdc++.h>
using namespace std;
vector<int> train[500069], bus[500069];
set<int> adj[500069];
int par[500069], sz[500069];
bool visited[500069];

void init(){
    for(int i=0; i<500069; i++){
        par[i] = i;
        sz[i] = 1;
        visited[i] = 0;
    }
}

int fp(int x){
    if(par[x]==x) return x;
    else return par[x]=fp(par[x]);
}

void merge(int x, int y){
    int px, py;
    px = fp(x), py = fp(y);
    if(sz[px] > sz[py]){
        par[py] = px;
        sz[px] += sz[py];
    } else {
        par[px] = py;
        sz[py] += sz[px];
    }
}

void search(int x){
    if(visited[x]) return;
    visited[x] = true;
    for(int i: train[x]){
        if(visited[i]) continue;
        merge(x,i);
        search(x);
    }
}

int main(){
    init();
    int n,m; cin >> n >> m;
    for(int i=0; i<n; i++){
        int a,b; char c;
        cin >> a >> b >> c;
        a--; b--;
        if(c=='A'){
            bus[a].push_back(b);
            bus[b].push_back(a);
        } else {
            train[a].push_back(b);
            train[b].push_back(a);
        }
    }
    for(int i=0; i<n; i++){
        search(i);
    }

    for(int i=0; i<n; i++){
        for(int j: bus[i]){
            int px = fp(i), py = fp(j);
            adj[px].insert(py);
            adj[py].insert(px);
        }
    }
    int ans = 0;
    set<int> pars;
    for(int i=0; i<n; i++){
        pars.insert(fp(i));
    }
    for(int i=0; i<n; i++){
        //if(adj[i].size() != 0) cout << i << " " << adj[i].size() << "\n";
        if(adj[i].size() == pars.size()-1) ans += sz[fp(i)];
    }
    cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 360 ms 62612 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 121 ms 134300 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 51676 KB Output is correct
2 Incorrect 27 ms 51668 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 51676 KB Output is correct
2 Incorrect 27 ms 51668 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 51676 KB Output is correct
2 Incorrect 27 ms 51668 KB Output isn't correct
3 Halted 0 ms 0 KB -