Submission #633492

# Submission time Handle Problem Language Result Execution time Memory
633492 2022-08-22T15:08:21 Z Ez0zIOVgTsSgT Flood (IOI07_flood) C++14
100 / 100
142 ms 30796 KB
#include<bits/stdc++.h>
using namespace std;

void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
    cerr << h; if(sizeof...(t)) cerr << ", ";
    DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL

#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define tcT template<class T
#define nl "\n"
#define fi first
#define se second

using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;

void setIO(string NAME = "") {
    cin.tie(0)->sync_with_stdio(0);
    if(sz(NAME)) {
        freopen((NAME + ".inp").c_str(),"r",stdin);
        freopen((NAME + ".out").c_str(),"w",stdout);
    }
}

tcT> bool ckmin(T&a, const T&b) {
    return b < a ? a=b,1 : 0; }
tcT> bool ckmax(T&a, const T&b) {
    return b > a ? a=b,1 : 0; }

const int MOD = 1e9 + 7;

const int MX = 2e5+5;
int N, W, enA[MX], enB[MX], X[MX], Y[MX];
pi pts[MX], adj[MX][4];
int reg[MX][4], cc;
vi surround[MX], adj1[MX];
int dist[MX], vis[MX];

int p;
pi corner;

int ang(int a, int b) {
    if(Y[a] == Y[b]) {
        if(X[a] < X[b]) return 0;
        else return 2;
    }
    else {
        if(Y[a] < Y[b]) return 1;
        else return 3;
    }
}

void dfs(int x, int y) {
    if(reg[x][y]) return;
    reg[x][y] = cc;
    surround[cc].pb(x);
    ROF(i,2,6) if(adj[adj[x][y].fi][(y+i)%4].se) {
        dfs(adj[x][y].fi, (i+y)%4);
        return;
    }
}

void dfs1(int x) {
    vis[x] = 1;
    each(ind, surround[x]) {
        if(ckmin(corner, {X[ind], Y[ind]})) {
            p = ind;
        }
    }
    each(y, adj1[x]) if(!vis[y]) dfs1(y);
}

void bfs(int src) {
    queue<int> q;
    q.push(src);
    dist[src] = 0;
    while(sz(q)) {
        int u = q.front();
        q.pop();
        each(v, adj1[u]) if(dist[v] == -1) {
            dist[v] = dist[u] + 1;
            q.push(v);
        }
    }
}

void solve() {
    cin >> N;
    FOR(i,1,N+1) {
        cin >> X[i] >> Y[i];
    }
    cin >> W;
    FOR(i,1,W+1) {
        cin >> enA[i] >> enB[i];
        adj[enA[i]][ang(enA[i], enB[i])] = {enB[i], i};
        adj[enB[i]][ang(enB[i], enA[i])] = {enA[i], i};
    }
    FOR(i,1,N+1) {
        F0R(j, 4) if(adj[i][j].se && !reg[i][j]) {
            cc++;
            dfs(i, j);
        }
    }
    FOR(i,1,W+1) {
        int u = reg[enA[i]][ang(enA[i], enB[i])];
        int v = reg[enB[i]][ang(enB[i], enA[i])];
        if(u != v) {
            adj1[u].pb(v);
            adj1[v].pb(u);
        }
    }
    memset(dist, -1, sizeof dist);
    FOR(i,1,cc+1) if(!vis[i]) {
        p = -1;
        corner = {MOD, MOD};
        dfs1(i);
        if(reg[p][1]) bfs(reg[p][1]);
        else bfs(reg[p][0]);
    }
    vi ans;
    FOR(i,1,W+1) {
        int u = reg[enA[i]][ang(enA[i], enB[i])];
        int v = reg[enB[i]][ang(enB[i], enA[i])];
        if(dist[u] == dist[v]) {
            ans.pb(i);
        }
    }
    cout << sz(ans) << nl;
    each(ind, ans) cout << ind << nl;
}

int main() {
    setIO();

    int t=1;
    //cin>>t;
    while(t-->0) {
        solve();
    }

    return 0;
}

Compilation message

flood.cpp: In function 'void setIO(std::string)':
flood.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |         freopen((NAME + ".inp").c_str(),"r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
flood.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |         freopen((NAME + ".out").c_str(),"w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 10452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10452 KB Output is correct
2 Correct 5 ms 10452 KB Output is correct
3 Correct 6 ms 10452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10452 KB Output is correct
2 Correct 5 ms 10540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10448 KB Output is correct
2 Correct 7 ms 10452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 10452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10452 KB Output is correct
2 Correct 7 ms 10580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 10580 KB Output is correct
2 Correct 6 ms 10504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 12628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 19628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 19068 KB Output is correct
2 Correct 131 ms 25308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 83 ms 21160 KB Output is correct
2 Correct 142 ms 26244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 22464 KB Output is correct
2 Correct 67 ms 30796 KB Output is correct