답안 #447979

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
447979 2021-07-28T10:56:39 Z zxcvbnm Traffic (CEOI11_tra) C++14
100 / 100
1114 ms 75648 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int nax = 3e5+5;
#define x first
#define y second
int n, m, a, b;
vector<int> adj[nax];
vector<int> radj[nax];
vector<pair<int, int>> p;
vector<int> from, to;
bool vis_west[nax];
bool vis_east[nax];
void dfs1(int v) {
    vis_east[v] = true;
    for(int u : adj[v]) {
        if (!vis_east[u]) {
            dfs1(u);
        }
    }
}
void dfs2(int v) {
    vis_west[v] = true;
    for(int u : radj[v]) {
        if (!vis_west[u]) {
            dfs2(u);
        }
    }
}

int south[nax], north[nax];
bool vis[nax];
vector<int> new_idx(nax);
void dfs_north(int v, int par) {
    vis[v] = true;
    for(int u : adj[v]) {
        if (p[u].x == a) {
            north[par] = max(north[par], new_idx[u]);
        }
        if (!vis[u]) {
            dfs_north(u, par);
        }
    }
}
void dfs_south(int v, int par) {
    vis[v] = true;
    for(int u : adj[v]) {
        if (p[u].x == a) {
            south[par] = min(south[par], new_idx[u]);
        }
        if (!vis[u]) {
            dfs_south(u, par);
        }
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> m >> a >> b;
    p.resize(n);
    for(int i = 0; i < n; i++) {
        cin >> p[i].x >> p[i].y;
        if (p[i].x == 0) {
            from.push_back(i);
        } else if (p[i].x == a) {
            to.push_back(i);
        }
    }

    for(int i = 0; i < m; i++) {
        int v1, v2, type;
        cin >> v1 >> v2 >> type;
        v1--, v2--;
        adj[v1].push_back(v2);
        radj[v2].push_back(v1);
        if (type == 2) {
            adj[v2].push_back(v1);
            radj[v1].push_back(v2);
        }
    }

    sort(from.begin(), from.end(), [&](const int& left, const int& right) {
        return p[left].y < p[right].y;
    });
    sort(to.begin(), to.end(), [&](const int& left, const int& right) {
        return p[left].y < p[right].y;
    });

    for(int i : from) {
        if (!vis_east[i]) {
            dfs1(i);
        }
    }

    vector<int> east;
    for(int i : to) {
        if (vis_east[i]) {
            east.push_back(i);
        }
    }

    for(int i : to) {
        if (!vis_west[i]) {
            dfs2(i);
        }
    }
    vector<int> west;
    for(int i : from) {
        if (vis_west[i]) {
            west.push_back(i);
        }
    }

    sort(east.begin(), east.end(), [&](const int& left, const int& right) {
        return p[left].y < p[right].y;
    });
    sort(west.begin(), west.end(), [&](const int& left, const int& right) {
        return p[left].y < p[right].y;
    });

    int idx = 0;
    for(int i : east) {
        new_idx[i] = idx++;
    }

    int sz = west.size();
    for(int i = sz-1; i >= 0; i--) {
        if (i != sz-1) {
            south[west[i]] = south[west[i+1]];
        } else {
            south[west[i]] = 2*n;
        }
        dfs_south(west[i], west[i]);
//        cout << "SOUTH: " << west[i]+1 << " " << south[west[i]]+1 << "\n";
    }

    fill(vis, vis+nax, false);
    for(int i = 0; i < sz; i++) {
        if (i != 0) {
            north[west[i]] = north[west[i-1]];
        } else {
            north[west[i]] = 0;
        }
        dfs_north(west[i], west[i]);
//        cout << "WEST: " << west[i]+1 << " " << north[west[i]]+1 << "\n";
    }


    idx = 0;
    vector<int> comp(n+1);
    for(int i : from) {
        comp[i] = idx++;
//        cout << east[i] << " " << new_idx[east[i]] << "\n";
    }

    vector<int> ans((int) from.size(), 0);
    for(int i : west) {
//        ans[comp[i]] = new_idx[north[i]] - new_idx[south[i]] + 1;
        ans[comp[i]] = north[i] - south[i] + 1;
//        cout << i << " " << north[i] << " " << south[i] << "\n";
    }

    reverse(ans.begin(), ans.end());
    for(int i : ans) {
        cout << i << "\n";
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 15820 KB Output is correct
2 Correct 9 ms 15772 KB Output is correct
3 Correct 9 ms 15836 KB Output is correct
4 Correct 9 ms 15828 KB Output is correct
5 Correct 9 ms 15820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 15864 KB Output is correct
2 Correct 9 ms 15780 KB Output is correct
3 Correct 9 ms 15848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 15908 KB Output is correct
2 Correct 9 ms 15948 KB Output is correct
3 Correct 9 ms 15948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 15988 KB Output is correct
2 Correct 14 ms 16320 KB Output is correct
3 Correct 15 ms 16104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 17100 KB Output is correct
2 Correct 63 ms 22272 KB Output is correct
3 Correct 39 ms 18744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 19396 KB Output is correct
2 Correct 118 ms 24260 KB Output is correct
3 Correct 69 ms 21324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 106 ms 22740 KB Output is correct
2 Correct 154 ms 30820 KB Output is correct
3 Correct 284 ms 29012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 197 ms 25456 KB Output is correct
2 Correct 173 ms 29712 KB Output is correct
3 Correct 282 ms 29676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 295 ms 30304 KB Output is correct
2 Correct 320 ms 39692 KB Output is correct
3 Correct 506 ms 41052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 450 ms 38212 KB Output is correct
2 Correct 532 ms 54436 KB Output is correct
3 Correct 542 ms 43328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1006 ms 54684 KB Output is correct
2 Correct 615 ms 56280 KB Output is correct
3 Correct 979 ms 58912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 227 ms 29764 KB Output is correct
2 Correct 652 ms 62036 KB Output is correct
3 Correct 849 ms 53496 KB Output is correct
4 Correct 1114 ms 75648 KB Output is correct