답안 #447980

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
447980 2021-07-28T10:57:50 Z zxcvbnm Traffic (CEOI11_tra) C++14
100 / 100
1127 ms 57788 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);
        }
    }

    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 15780 KB Output is correct
3 Correct 9 ms 15820 KB Output is correct
4 Correct 9 ms 15820 KB Output is correct
5 Correct 9 ms 15820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 15820 KB Output is correct
2 Correct 9 ms 15820 KB Output is correct
3 Correct 9 ms 15820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 15820 KB Output is correct
2 Correct 10 ms 15940 KB Output is correct
3 Correct 9 ms 15888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 15948 KB Output is correct
2 Correct 13 ms 16348 KB Output is correct
3 Correct 13 ms 16200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 17092 KB Output is correct
2 Correct 61 ms 20756 KB Output is correct
3 Correct 40 ms 17860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 19044 KB Output is correct
2 Correct 80 ms 22468 KB Output is correct
3 Correct 53 ms 19964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 21776 KB Output is correct
2 Correct 151 ms 27632 KB Output is correct
3 Correct 251 ms 24448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 163 ms 23552 KB Output is correct
2 Correct 184 ms 26508 KB Output is correct
3 Correct 294 ms 24940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 306 ms 27756 KB Output is correct
2 Correct 323 ms 33728 KB Output is correct
3 Correct 547 ms 31732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 403 ms 34500 KB Output is correct
2 Correct 518 ms 44580 KB Output is correct
3 Correct 553 ms 33948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 974 ms 46096 KB Output is correct
2 Correct 564 ms 45868 KB Output is correct
3 Correct 969 ms 42312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 220 ms 28460 KB Output is correct
2 Correct 624 ms 51136 KB Output is correct
3 Correct 844 ms 39876 KB Output is correct
4 Correct 1127 ms 57788 KB Output is correct