답안 #1027194

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1027194 2024-07-19T01:56:37 Z KhoaDuy One-Way Streets (CEOI17_oneway) C++14
100 / 100
157 ms 67412 KB
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
const int MAXN=2*1e5;
int comp[MAXN+1]={0};
int tim=1;
int low[MAXN+1];
int in[MAXN+1]={0};
int mark[MAXN+1]={0};
vector<vector<int>> graph(MAXN+1);
vector<vector<int>> idx(MAXN+1);
char ans[MAXN+1];
int cc=0;
stack<int> st;
void DFS(int u,int p){
    in[u]=tim;
    low[u]=tim;
    tim++;
    bool skip=false;
    st.push(u);
    for(int i=0;i<graph[u].size();i++){
        int v=graph[u][i];
        if(v==p&&!skip){
            skip=true;
            continue;
        }
        if(!in[v]){
            DFS(v,u);
            low[u]=min(low[u],low[v]);
            if(in[u]<low[v]){
                while(st.top()!=v){
                    comp[st.top()]=cc;
                    st.pop();
                }
                comp[st.top()]=cc;
                st.pop();
                cc++;
            }
            else{
                ans[idx[u][i]]='B';
            }
        }
        else{
            ans[idx[u][i]]='B';
            low[u]=min(low[u],in[v]);
        }
    }
}
int lift[19][MAXN+1];
int depth[MAXN+1];
void setup(int u,int p,int idx){
    lift[0][u]=p;
    mark[u]=idx;
    for(int i=1;i<19;i++){
        lift[i][u]=lift[i-1][lift[i-1][u]];
    }
    for(int i=0;i<graph[u].size();i++){
        int v=graph[u][i];
        if(v!=p){
            depth[v]=depth[u]+1;
            setup(v,u,idx);
        }
    }
}
int a[MAXN+1]={0};
int b[MAXN+1]={0};
int c[MAXN+1]={0};
int LCA(int u,int v){
    if(depth[u]<depth[v]){
        swap(u,v);
    }
    for(int i=18;i>=0;i--){
        if((depth[u]-depth[v])&(1<<i)){
            u=lift[i][u];
        }
    }
    if(u==v){
        return u;
    }
    for(int i=18;i>=0;i--){
        if(lift[i][u]!=lift[i][v]){
            u=lift[i][u];
            v=lift[i][v];
        }
    }
    return lift[0][u];
}
bool check(int u,int p){
    bool curr=true;
    for(int i=0;i<graph[u].size();i++){
        int v=graph[u][i];
        if(v!=p){
            curr&=check(v,u);
            a[u]+=a[v];
            b[u]+=b[v];
            c[u]+=c[v];
            if(a[v]==c[v]&&b[v]==c[v]){
                ans[abs(idx[u][i])-1]='B';
            }
            else if(b[v]>c[v]){
                if(idx[u][i]>0){
                    ans[abs(idx[u][i])-1]='R';
                }
                else{
                    ans[abs(idx[u][i])-1]='L';
                }
            }
            else{
                if(idx[u][i]>0){
                    ans[abs(idx[u][i])-1]='L';
                }
                else{
                    ans[abs(idx[u][i])-1]='R';
                }
            }
        }
    }
    if(a[u]>c[u]&&b[u]>c[u]){
        curr=false;
    }
    return curr;
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    vector<int> root;
    int n,m,q;
    cin >> n >> m;
    vector<pair<int,int>> edges(m);
    for(int i=0;i<m;i++){
        int u,v;
        cin >> u >> v;
        edges[i]={u,v};
        graph[u].push_back(v);
        graph[v].push_back(u);
        idx[u].push_back(i);
        idx[v].push_back(i);
    }
    for(int i=1;i<=n;i++){
        if(!in[i]){
            DFS(i,-1);
            root.push_back(i);
            while(!st.empty()){
                comp[st.top()]=cc;
                st.pop();
            }
            cc++;
        }
    }
    graph.clear();
    graph.resize(cc);
    idx.clear();
    idx.resize(cc);
    for(int i=0;i<m;i++){
        int u=edges[i].first,v=edges[i].second;
        if(comp[u]!=comp[v]){
            graph[comp[u]].push_back(comp[v]);
            graph[comp[v]].push_back(comp[u]);
            idx[comp[u]].push_back(i+1);
            idx[comp[v]].push_back(-(i+1));
        }
    }
    int idx=0;
    for(int i=0;i<root.size();i++){
        depth[comp[root[i]]]=0;
        setup(comp[root[i]],comp[root[i]],idx);
        idx++;
    }
    cin >> q;
    bool flag=true;
    for(int i=0;i<q;i++){
        int s,d;
        cin >> s >> d;
        if(!flag||mark[comp[s]]!=mark[comp[d]]){
            flag=false;
            continue;
        }
        int lca=LCA(comp[s],comp[d]);
        a[comp[s]]++;
        b[comp[d]]++;
        c[lca]++;
    }
    for(int i=0;i<root.size();i++){
        flag&=check(comp[root[i]],-1);
    }
    for(int i=0;i<m;i++){
        cout << ans[i];
    }
}

Compilation message

oneway.cpp: In function 'void DFS(long long int, long long int)':
oneway.cpp:22:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |     for(int i=0;i<graph[u].size();i++){
      |                 ~^~~~~~~~~~~~~~~~
oneway.cpp: In function 'void setup(long long int, long long int, long long int)':
oneway.cpp:58:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |     for(int i=0;i<graph[u].size();i++){
      |                 ~^~~~~~~~~~~~~~~~
oneway.cpp: In function 'bool check(long long int, long long int)':
oneway.cpp:91:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |     for(int i=0;i<graph[u].size();i++){
      |                 ~^~~~~~~~~~~~~~~~
oneway.cpp: In function 'int main()':
oneway.cpp:165:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  165 |     for(int i=0;i<root.size();i++){
      |                 ~^~~~~~~~~~~~
oneway.cpp:184:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  184 |     for(int i=0;i<root.size();i++){
      |                 ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 48728 KB Output is correct
2 Correct 5 ms 48792 KB Output is correct
3 Correct 6 ms 48788 KB Output is correct
4 Correct 6 ms 48732 KB Output is correct
5 Correct 7 ms 48932 KB Output is correct
6 Correct 7 ms 48988 KB Output is correct
7 Correct 6 ms 48988 KB Output is correct
8 Correct 6 ms 48728 KB Output is correct
9 Correct 6 ms 48728 KB Output is correct
10 Correct 6 ms 48728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 48728 KB Output is correct
2 Correct 5 ms 48792 KB Output is correct
3 Correct 6 ms 48788 KB Output is correct
4 Correct 6 ms 48732 KB Output is correct
5 Correct 7 ms 48932 KB Output is correct
6 Correct 7 ms 48988 KB Output is correct
7 Correct 6 ms 48988 KB Output is correct
8 Correct 6 ms 48728 KB Output is correct
9 Correct 6 ms 48728 KB Output is correct
10 Correct 6 ms 48728 KB Output is correct
11 Correct 38 ms 58196 KB Output is correct
12 Correct 39 ms 59476 KB Output is correct
13 Correct 51 ms 60808 KB Output is correct
14 Correct 58 ms 62292 KB Output is correct
15 Correct 62 ms 62544 KB Output is correct
16 Correct 84 ms 60500 KB Output is correct
17 Correct 76 ms 62280 KB Output is correct
18 Correct 77 ms 60480 KB Output is correct
19 Correct 78 ms 63828 KB Output is correct
20 Correct 42 ms 59112 KB Output is correct
21 Correct 41 ms 58708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 48728 KB Output is correct
2 Correct 5 ms 48792 KB Output is correct
3 Correct 6 ms 48788 KB Output is correct
4 Correct 6 ms 48732 KB Output is correct
5 Correct 7 ms 48932 KB Output is correct
6 Correct 7 ms 48988 KB Output is correct
7 Correct 6 ms 48988 KB Output is correct
8 Correct 6 ms 48728 KB Output is correct
9 Correct 6 ms 48728 KB Output is correct
10 Correct 6 ms 48728 KB Output is correct
11 Correct 38 ms 58196 KB Output is correct
12 Correct 39 ms 59476 KB Output is correct
13 Correct 51 ms 60808 KB Output is correct
14 Correct 58 ms 62292 KB Output is correct
15 Correct 62 ms 62544 KB Output is correct
16 Correct 84 ms 60500 KB Output is correct
17 Correct 76 ms 62280 KB Output is correct
18 Correct 77 ms 60480 KB Output is correct
19 Correct 78 ms 63828 KB Output is correct
20 Correct 42 ms 59112 KB Output is correct
21 Correct 41 ms 58708 KB Output is correct
22 Correct 157 ms 63360 KB Output is correct
23 Correct 121 ms 61524 KB Output is correct
24 Correct 114 ms 61520 KB Output is correct
25 Correct 129 ms 67412 KB Output is correct
26 Correct 134 ms 63056 KB Output is correct
27 Correct 120 ms 61660 KB Output is correct
28 Correct 26 ms 51892 KB Output is correct
29 Correct 55 ms 59824 KB Output is correct
30 Correct 71 ms 59988 KB Output is correct
31 Correct 55 ms 60664 KB Output is correct
32 Correct 88 ms 62544 KB Output is correct