Submission #969615

# Submission time Handle Problem Language Result Execution time Memory
969615 2024-04-25T11:07:56 Z hirayuu_oj Flood (IOI07_flood) C++17
100 / 100
95 ms 16072 KB
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
#define all(x) x.begin(),x.end()
using ll=long long;
 
 
int dir(pair<int,int> x,pair<int,int> y){
    if(x.first==y.first){
        if(x.second<y.second)return 0;
        return 2;
    }
    if(x.first<y.first)return 1;
    return 3;
}
int wall_ind(array<pair<int,int>,4> wall){
    int cnt=0;
    int pos=-1;
    rep(i,4){
        if(wall[i].first!=-1){
            cnt++;
            pos=i;
        }
    }
    if(cnt==1)return pos;
    return -1;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin>>N;
    vector<pair<int,int>> pnt(N);
    rep(i,N){
        cin>>pnt[i].first>>pnt[i].second;
    }
    vector<array<pair<int,int>,4>> gr(N,{pair(-1,-1),pair(-1,-1),pair(-1,-1),pair(-1,-1)});
    int W;
    cin>>W;
    vector<pair<int,int>> wall(W);
    rep(i,W){
        cin>>wall[i].first>>wall[i].second;
        wall[i].first--;
        wall[i].second--;
        gr[wall[i].first][dir(pnt[wall[i].first],pnt[wall[i].second])]=pair(i,wall[i].second);
        gr[wall[i].second][dir(pnt[wall[i].second],pnt[wall[i].first])]=pair(i,wall[i].first);
    }
    vector<int> ans;
    vector<array<int,4>> vert(N,{-1,-1,-1,-1});
    int cnt=0;
    rep(i,N){
        rep(j,4){
            if(gr[i][j].first==-1)continue;
            if(vert[i][j]!=-1)continue;
            int pos=i;
            int dir=j;
            while(vert[pos][dir]==-1){
                vert[pos][dir]=cnt;
                pos=gr[pos][dir].second;
                dir=(dir+3)%4;
                while(gr[pos][dir].first==-1){
                    dir++;
                    dir%=4;
                }
            }
            assert(vert[pos][dir]==cnt);
            cnt++;
        }
    }
    vector<vector<int>> zag(cnt);
    rep(i,W){
        int s=vert[wall[i].first][dir(pnt[wall[i].first],pnt[wall[i].second])];
        int t=vert[wall[i].second][dir(pnt[wall[i].second],pnt[wall[i].first])];
        zag[s].push_back(t);
        zag[t].push_back(s);
    }
    vector<pair<int,int>> order;
    rep(i,W){
        if(dir(pnt[wall[i].first],pnt[wall[i].second])==3){
            order.push_back({pnt[wall[i].first].second,vert[wall[i].first][3]});
        }
        if(dir(pnt[wall[i].second],pnt[wall[i].first])==3){
            order.push_back({pnt[wall[i].second].second,vert[wall[i].second][3]});
        }
        if(dir(pnt[wall[i].first],pnt[wall[i].second])==0){
            order.push_back({pnt[wall[i].first].first,vert[wall[i].first][0]});
        }
        if(dir(pnt[wall[i].second],pnt[wall[i].first])==0){
            order.push_back({pnt[wall[i].second].first,vert[wall[i].second][0]});
        }
    }
    sort(all(order));
    vector<int> dist(cnt,-1);
    for(auto &[_,p]:order){
        if(dist[p]!=-1)continue;
        dist[p]=0;
        queue<int> pos;
        pos.push(p);
        while(!pos.empty()){
            int np=pos.front();
            pos.pop();
            for(int i:zag[np]){
                if(dist[i]==-1){
                    dist[i]=dist[np]+1;
                    pos.push(i);
                }
            }
        }
    }
    rep(i,W){
        int s=vert[wall[i].first][dir(pnt[wall[i].first],pnt[wall[i].second])];
        int t=vert[wall[i].second][dir(pnt[wall[i].second],pnt[wall[i].first])];
        if(dist[s]==dist[t]){
            ans.push_back(i);
        }
    }
    sort(all(ans));
    ans.erase(unique(all(ans)),ans.end());
    cout<<ans.size()<<"\n";
    for(int i:ans)cout<<i+1<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 3160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 10076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 11980 KB Output is correct
2 Correct 80 ms 15400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 13828 KB Output is correct
2 Correct 91 ms 16072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 95 ms 15284 KB Output is correct
2 Correct 56 ms 12144 KB Output is correct