답안 #856450

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
856450 2023-10-03T14:09:46 Z 8pete8 One-Way Streets (CEOI17_oneway) C++14
100 / 100
125 ms 50152 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define pb push_back
#define p push
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
//#define int long long
const int mxn=2*1e5,mod=29947,lg=30;
vector<pii>adj[mxn+10],e,adj2[mxn+10];
int low[mxn+10],g[mxn+10],id[mxn+10];
int up[mxn+10][lg+10],h[mxn+10],ans[mxn+10];
pii info[mxn+10];
stack<int>st;
bitset<mxn+10>on;
int t=0,cnt=0;
void tarjan(int cur,int pa){
    id[cur]=low[cur]=++t;
    on[cur]=true;
    st.p(cur);
    for(auto i:adj[cur]){
        if(!id[i.f]){
            tarjan(i.f,i.s);
            low[cur]=min(low[cur],low[i.f]);
        }
        else if(i.s!=pa)low[cur]=min(low[cur],low[i.f]);
    }
    if(low[cur]==id[cur]){
        cnt++;
        while(st.top()!=cur){
            g[st.top()]=cnt;
            on[st.top()]=false;
            st.pop();
        }
        on[st.top()]=false;
        g[st.top()]=cnt;
        st.pop();
    }
}
void dfs(int cur,int pa){
    on[cur]=true;
    for(auto i:adj2[cur]){
        if(i.f==pa)continue;
        if(on[i.f]){
            continue;
        }
        up[i.f][0]=cur;
        h[i.f]=h[cur]+1;
        dfs(i.f,cur);
    }
}
int lca(int a,int b){
    if(h[a]<h[b])swap(a,b);
    int k=h[a]-h[b];
    for(int i=0;i<=lg;i++)if(k&(1<<i))a=up[a][i];
    if(a==b)return a;
    for(int i=lg;i>=0;i--){
        if(up[a][i]!=up[b][i]){
            a=up[a][i];
            b=up[b][i];
        }
    }
    return up[a][0];
}
//ans 1=right,2 left
void push(int cur,int pa){
    if(on[cur])return;
    on[cur]=true;
    for(auto i:adj2[cur]){
        if(i.f==pa)continue;
        push(i.f,cur);
        if(info[i.f].f){
            if(info[i.f].f!=cur&&(!info[cur].f||h[info[i.f].f]<h[info[cur].f]))info[cur]=info[i.f];
            if(info[i.f].s){
                if(g[e[i.s].f]==i.f)ans[i.s]=1;
                else ans[i.s]=2;
            }
            else{
                if(g[e[i.s].f]==cur)ans[i.s]=1;
                else ans[i.s]=2;
            }
        }
    }
}
int32_t main(){
    fastio
    int n,m;cin>>n>>m;
    for(int i=0;i<m;i++){
        int u,v;cin>>u>>v;
        e.pb({u,v});
        adj[v].pb({u,i});
        adj[u].pb({v,i});
    }
    for(int i=1;i<=n;i++)if(!g[i])tarjan(i,-1);
    for(int i=1;i<=n;i++)for(auto j:adj[i])if(g[j.f]!=g[i])adj2[g[i]].pb({g[j.f],j.s});
    on.reset();
    for(int i=1;i<=cnt;i++)if(!on[i])dfs(i,-1);
    for(int j=1;j<=lg;j++)for(int i=1;i<=cnt;i++)up[i][j]=up[up[i][j-1]][j-1];
    int k;cin>>k;
    for(int i=0;i<k;i++){
        int u,v;cin>>u>>v;
        int a=u,b=v;
        u=g[u],v=g[v];
        if(u==v)continue;
        int node=lca(u,v);
        if(u!=node&&(info[u].f==0||h[info[u].f]>h[node]))info[u].f=node;
        if(v!=node&&(info[v].f==0||h[info[v].f]>h[node]))info[v].f=node;
        if(u!=node)info[u].s=1;
    }
    on.reset();
    for(int i=1;i<=cnt;i++)if(!on[i])push(i,-1);
    for(int i=0;i<m;i++){
        if(ans[i])cout<<((ans[i]==1)?"R":"L");
        else cout<<"B";
    }
}

Compilation message

oneway.cpp: In function 'int32_t main()':
oneway.cpp:124:13: warning: unused variable 'a' [-Wunused-variable]
  124 |         int a=u,b=v;
      |             ^
oneway.cpp:124:17: warning: unused variable 'b' [-Wunused-variable]
  124 |         int a=u,b=v;
      |                 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 17752 KB Output is correct
2 Correct 3 ms 18008 KB Output is correct
3 Correct 3 ms 17756 KB Output is correct
4 Correct 4 ms 18008 KB Output is correct
5 Correct 3 ms 17756 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 3 ms 17756 KB Output is correct
8 Correct 4 ms 17852 KB Output is correct
9 Correct 4 ms 17756 KB Output is correct
10 Correct 4 ms 17756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 17752 KB Output is correct
2 Correct 3 ms 18008 KB Output is correct
3 Correct 3 ms 17756 KB Output is correct
4 Correct 4 ms 18008 KB Output is correct
5 Correct 3 ms 17756 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 3 ms 17756 KB Output is correct
8 Correct 4 ms 17852 KB Output is correct
9 Correct 4 ms 17756 KB Output is correct
10 Correct 4 ms 17756 KB Output is correct
11 Correct 28 ms 21960 KB Output is correct
12 Correct 31 ms 25284 KB Output is correct
13 Correct 34 ms 26320 KB Output is correct
14 Correct 44 ms 29472 KB Output is correct
15 Correct 44 ms 30920 KB Output is correct
16 Correct 61 ms 41668 KB Output is correct
17 Correct 63 ms 44196 KB Output is correct
18 Correct 66 ms 42652 KB Output is correct
19 Correct 64 ms 46276 KB Output is correct
20 Correct 31 ms 24516 KB Output is correct
21 Correct 29 ms 24016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 17752 KB Output is correct
2 Correct 3 ms 18008 KB Output is correct
3 Correct 3 ms 17756 KB Output is correct
4 Correct 4 ms 18008 KB Output is correct
5 Correct 3 ms 17756 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 3 ms 17756 KB Output is correct
8 Correct 4 ms 17852 KB Output is correct
9 Correct 4 ms 17756 KB Output is correct
10 Correct 4 ms 17756 KB Output is correct
11 Correct 28 ms 21960 KB Output is correct
12 Correct 31 ms 25284 KB Output is correct
13 Correct 34 ms 26320 KB Output is correct
14 Correct 44 ms 29472 KB Output is correct
15 Correct 44 ms 30920 KB Output is correct
16 Correct 61 ms 41668 KB Output is correct
17 Correct 63 ms 44196 KB Output is correct
18 Correct 66 ms 42652 KB Output is correct
19 Correct 64 ms 46276 KB Output is correct
20 Correct 31 ms 24516 KB Output is correct
21 Correct 29 ms 24016 KB Output is correct
22 Correct 114 ms 45252 KB Output is correct
23 Correct 125 ms 43716 KB Output is correct
24 Correct 93 ms 43968 KB Output is correct
25 Correct 102 ms 50152 KB Output is correct
26 Correct 103 ms 45252 KB Output is correct
27 Correct 100 ms 43732 KB Output is correct
28 Correct 27 ms 17616 KB Output is correct
29 Correct 49 ms 24992 KB Output is correct
30 Correct 47 ms 25168 KB Output is correct
31 Correct 44 ms 25536 KB Output is correct
32 Correct 64 ms 31432 KB Output is correct