답안 #410608

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
410608 2021-05-23T07:12:11 Z Aldas25 Flood (IOI07_flood) C++14
91 / 100
2000 ms 28708 KB
#pragma GCC optimize("O2")

#include <bits/stdc++.h>

using namespace std;

#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(0)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define pb push_back
#define f first
#define s second
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<piii> viii;

const int MAXN = 200100, MAXK = 30;
//const ll MOD = 998244353;
const ll INF = 1e17;
const ld PI = asin(1) * 2;

int n;
int x[MAXN], y[MAXN];
int w;
int a[MAXN], b[MAXN];
unordered_set<int> ans;
int deg[MAXN];

vector<pair<pii, int>> avPoints;
bool usedPoint[MAXN];
pii adj[MAXN][4];
bool vis[MAXN][4];
unordered_set<int> curWals;
//vii toEr;

void dfs (int id, int dir) {
    if (vis[id][dir]) return;
    vis[id][dir] = true;

   // cout << "    dfs in id = " << id << " dir = " << dir <<  "  x = " << x[id] << " y = " << y[id] << endl;

    FOR(a, -1, 2) {
        int newDir = (dir + a + 4) % 4;
        if (adj[id][newDir].f == -1) continue;
        int pId = adj[id][newDir].f;
        int wId = adj[id][newDir].s;

        if (!vis[pId][newDir]) {
            if (curWals.count(wId))
                ans.insert(wId);
            else
                curWals.insert(wId);
            dfs(pId, newDir);
        }
        break;
    }


}

int main()
{
    FAST_IO;

    cin >> n;
    FOR(i, 1, n) cin >> x[i] >> y[i];
    cin >> w;
    FOR(i, 1, w) cin >> a[i] >> b[i];

    FOR(i, 1, n) avPoints.pb({{x[i], y[i]}, i});
    sort(avPoints.begin(), avPoints.end());
    FOR(i, 1, n) FOR(d, 0, 3) adj[i][d] = {-1, -1};

    FOR(i, 1, w) {
        if (x[a[i]] > x[b[i]] || y[a[i]] > y[b[i]]) swap(a[i], b[i]);
        if (y[a[i]] == y[b[i]]) {
            adj[a[i]][1] = {b[i], i};
            adj[b[i]][3] = {a[i], i};
        } else {
            adj[a[i]][0] = {b[i], i};
            adj[b[i]][2] = {a[i], i};
        }

        deg[a[i]]++;
        deg[b[i]]++;
    }

    for (auto p : avPoints) {
        //auto p = *(avPoints.begin());
        curWals.clear();
        int id = p.s;
        if (usedPoint[id]) continue;
        //toEr.clear();

       // cout << " starting walk from id = " << id << " x = " << x[id] << " y = " << y[id] << endl;

        int stDir = 0;
        while (vis[id][stDir]) {stDir++;}
        dfs (id, stDir);

        for (auto wall : curWals) {
            int i = wall;
            if (y[a[i]] == y[b[i]]) {
                adj[a[i]][1] = {-1, -1};
                adj[b[i]][3] = {-1, -1};
            } else {
                adj[a[i]][0] = {-1, -1};
                adj[b[i]][2] = {-1, -1};
            }
            deg[a[i]]--;
            deg[b[i]]--;

            int id;
            id = a[i];
            if (deg[id] == 0)
                usedPoint[id] = true;
            id = b[i];
            if (deg[id] == 0)
                usedPoint[id] = true;
        }
    }

    cout << (int)ans.size() << "\n";
    for (int wall : ans) cout << wall << "\n";

    return 0;
}

/*

15
1 1
8 1
4 2
7 2
2 3
4 3
6 3
2 5
4 5
6 5
4 6
7 6
1 8
4 8
8 8
17
1 2
2 15
15 14
14 13
13 1
14 11
11 12
12 4
4 3
3 6
6 5
5 8
8 9
9 11
9 10
10 7
7 6

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 2596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 713 ms 10540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 6456 KB Output is correct
2 Correct 409 ms 10616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 242 ms 10292 KB Output is correct
2 Correct 126 ms 10308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 259 ms 10344 KB Output is correct
2 Execution timed out 2081 ms 28708 KB Time limit exceeded