Submission #566443

# Submission time Handle Problem Language Result Execution time Memory
566443 2022-05-22T10:12:22 Z BJoozz Alternating Current (BOI18_alternating) C++14
100 / 100
106 ms 16112 KB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define X first
#define Y second
#define int long long
void maxx(int &a,int b){if(b>a) a=b;}
void minn(int &a,int b){if(b<a) a=b;}
mt19937 rng(time(0));
int randint(int l,int r){
    return uniform_int_distribution < int > (l,r) (rng);
}
using ll = long long;
using ii = pair < int , int >;
const int MAX=1e5+3,inf=1e9,mod=1e9+7;
using ld = long double;
int n,m;
vector < int > pr[MAX];
int C[MAX];
bool vs[MAX];
bool in[MAX],ok[MAX];
bool good[MAX];
int L[MAX],R[MAX];
ii trace[MAX];
bool res[MAX];
int dis(int u,int v){
    return (v+n-u)%n+1;
}
bool cmp(int u,int v,int z){
    return dis(u,v)+dis(v,z)-1==dis(u,z);
}
signed main(){
    //freopen("Alternating Current.inp","r",stdin);freopen("Alternating Current.out","w",stdout);
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n>>m;
    int U=1,V=1;
    map < ii , int > mp;
    for(int i=1,x,y;i<=m;i++){
        cin>>x>>y;
        mp[ii(x,y)]=i;
        pr[x].pb(y);
        if(dis(U,V)<dis(x,y)){
            U=x;V=y;
        }
        if(x<=y){
            C[x]++;C[y+1]--;
        }else{
            C[x]++;C[1]++;C[y+1]--;
        }
    }
    for(int i=1;i<=n;i++)
    {
        C[i]+=C[i-1];
        if(C[i]<2){
            cout<<"impossible";return 0;
        }
        if(C[i]>2) ok[i]=1;
    }
    for(int i=1;i<=n;i++) L[i]=i-1,R[i]=i+1;

    L[1]=n;R[n]=1;
    vector < ii > vec;
    queue < int > Q;

    if(R[V]==U){
            res[mp[ii(U,V)]]=1;
        for(int i=1;i<=m;i++) cout<<res[i];//cout<<'\n';
        //getans(vec);
        return 0;
    }
    bool las=1;
    for(int i=U;i!=R[V];i=R[i]){
        in[i]=1;
        good[i]=las;
        las&=ok[i];
    }
    //for(int i=1;i<=n;i++)cout<<C[i]<<' ';cout<<'\n';

    int dich=-1;
    //cout<<U<<' '<<V<<'\n';
    Q.push(R[V]);vs[R[V]]=1;trace[R[V]]=ii(U,V);
    ii pd;
    while(!Q.empty() && dich==-1){
        int p = Q.front();Q.pop();
        //cout<<p<<'\n';
        for(int u:pr[p]){
            u=R[u];
            //cout<<p<<' '<<u<<'\n';
            if(in[u]){
                if(!in[p] &&good[u]){
                    pd=ii(p,L[u]);
                    dich=p;break;
                }
                if(in[p] && good[u] && cmp(U,u,p)){
                    pd=ii(p,L[u]);
                    dich=p;break;
                }
                continue;
            }
            if(!vs[u]){
                vs[u]=1;trace[u]=ii(p,L[u]);
                Q.push(u);
            }
        }
        //cout<<L[p]<<' '<<ok[7]<<' '<<vs[7]<<'\n';
        //cout<<trace[p].X<<'\n';
        if(ok[L[p]] && !vs[L[p]] && L[p]!=trace[p].X){
            vs[L[p]]=1;
            trace[L[p]]=trace[p];
            Q.push(L[p]);
        }
    }
    //cout<<dich<<'\n';
    if(dich==-1){
        cout<<"impossible";return 0;
    }
    int numQ=m;
    //cout<<pd.X<<' '<<pd.Y<<'\n';
    res[mp[pd]]=1;
    while(numQ--){
        ii now=trace[dich];
        //cout<<now.X<<' '<<now.Y<<'\n';
        res[mp[now]]=1;
        dich=now.X;
        if(now==ii(U,V)) break;
    }
    /// U pd.Y dich
    if(pd.Y==L[U] || dich==R[V] ||
       dis(U,pd.Y)+dis(dich,V)<=dis(U,V)){

    }else{
        //res[mp[ii(U,V)]]=0;
    }
    for(int i=1;i<=m;i++) cout<<res[i];//cout<<'\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2680 KB Output is correct
9 Correct 2 ms 2680 KB Output is correct
10 Correct 2 ms 2684 KB Output is correct
11 Correct 2 ms 2676 KB Output is correct
12 Correct 2 ms 2676 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2680 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2680 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2684 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2676 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2680 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 1 ms 2644 KB Output is correct
33 Correct 2 ms 2644 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 2 ms 2644 KB Output is correct
40 Correct 1 ms 2680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2680 KB Output is correct
9 Correct 2 ms 2680 KB Output is correct
10 Correct 2 ms 2684 KB Output is correct
11 Correct 2 ms 2676 KB Output is correct
12 Correct 2 ms 2676 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2680 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2680 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2684 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2676 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2680 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 1 ms 2644 KB Output is correct
33 Correct 2 ms 2644 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 2 ms 2644 KB Output is correct
40 Correct 1 ms 2680 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 2 ms 2680 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 2 ms 2644 KB Output is correct
47 Correct 1 ms 2656 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 2 ms 2644 KB Output is correct
50 Correct 2 ms 2644 KB Output is correct
51 Correct 2 ms 2644 KB Output is correct
52 Correct 2 ms 2676 KB Output is correct
53 Correct 2 ms 2644 KB Output is correct
54 Correct 2 ms 2644 KB Output is correct
55 Correct 2 ms 2644 KB Output is correct
56 Correct 2 ms 2684 KB Output is correct
57 Correct 2 ms 2644 KB Output is correct
58 Correct 2 ms 2644 KB Output is correct
59 Correct 2 ms 2644 KB Output is correct
60 Correct 2 ms 2644 KB Output is correct
61 Correct 2 ms 2644 KB Output is correct
62 Correct 2 ms 2644 KB Output is correct
63 Correct 2 ms 2644 KB Output is correct
64 Correct 2 ms 2644 KB Output is correct
65 Correct 2 ms 2644 KB Output is correct
66 Correct 2 ms 2680 KB Output is correct
67 Correct 2 ms 2644 KB Output is correct
68 Correct 2 ms 2644 KB Output is correct
69 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2680 KB Output is correct
9 Correct 2 ms 2680 KB Output is correct
10 Correct 2 ms 2684 KB Output is correct
11 Correct 2 ms 2676 KB Output is correct
12 Correct 2 ms 2676 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2680 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2680 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2684 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2676 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2680 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 1 ms 2644 KB Output is correct
33 Correct 2 ms 2644 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 2 ms 2644 KB Output is correct
40 Correct 1 ms 2680 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 2 ms 2680 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 2 ms 2644 KB Output is correct
47 Correct 1 ms 2656 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 2 ms 2644 KB Output is correct
50 Correct 2 ms 2644 KB Output is correct
51 Correct 2 ms 2644 KB Output is correct
52 Correct 2 ms 2676 KB Output is correct
53 Correct 2 ms 2644 KB Output is correct
54 Correct 2 ms 2644 KB Output is correct
55 Correct 2 ms 2644 KB Output is correct
56 Correct 2 ms 2684 KB Output is correct
57 Correct 2 ms 2644 KB Output is correct
58 Correct 2 ms 2644 KB Output is correct
59 Correct 2 ms 2644 KB Output is correct
60 Correct 2 ms 2644 KB Output is correct
61 Correct 2 ms 2644 KB Output is correct
62 Correct 2 ms 2644 KB Output is correct
63 Correct 2 ms 2644 KB Output is correct
64 Correct 2 ms 2644 KB Output is correct
65 Correct 2 ms 2644 KB Output is correct
66 Correct 2 ms 2680 KB Output is correct
67 Correct 2 ms 2644 KB Output is correct
68 Correct 2 ms 2644 KB Output is correct
69 Correct 2 ms 2644 KB Output is correct
70 Correct 2 ms 2644 KB Output is correct
71 Correct 2 ms 2772 KB Output is correct
72 Correct 2 ms 2644 KB Output is correct
73 Correct 2 ms 2644 KB Output is correct
74 Correct 2 ms 2644 KB Output is correct
75 Correct 2 ms 2680 KB Output is correct
76 Correct 2 ms 2656 KB Output is correct
77 Correct 2 ms 2772 KB Output is correct
78 Correct 2 ms 2644 KB Output is correct
79 Correct 2 ms 2644 KB Output is correct
80 Correct 2 ms 2772 KB Output is correct
81 Correct 2 ms 2772 KB Output is correct
82 Correct 2 ms 2772 KB Output is correct
83 Correct 2 ms 2772 KB Output is correct
84 Correct 2 ms 2772 KB Output is correct
85 Correct 2 ms 2644 KB Output is correct
86 Correct 2 ms 2680 KB Output is correct
87 Correct 2 ms 2900 KB Output is correct
88 Correct 2 ms 2644 KB Output is correct
89 Correct 2 ms 2644 KB Output is correct
90 Correct 3 ms 2772 KB Output is correct
91 Correct 2 ms 2644 KB Output is correct
92 Correct 2 ms 2644 KB Output is correct
93 Correct 2 ms 2644 KB Output is correct
94 Correct 2 ms 2680 KB Output is correct
95 Correct 2 ms 2644 KB Output is correct
96 Correct 2 ms 2772 KB Output is correct
97 Correct 2 ms 2772 KB Output is correct
98 Correct 2 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 6104 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 20 ms 8068 KB Output is correct
4 Correct 25 ms 9812 KB Output is correct
5 Correct 91 ms 14664 KB Output is correct
6 Correct 77 ms 11788 KB Output is correct
7 Correct 92 ms 15044 KB Output is correct
8 Correct 5 ms 6996 KB Output is correct
9 Correct 4 ms 5460 KB Output is correct
10 Correct 101 ms 16112 KB Output is correct
11 Correct 83 ms 14160 KB Output is correct
12 Correct 81 ms 14464 KB Output is correct
13 Correct 2 ms 3444 KB Output is correct
14 Correct 2 ms 3412 KB Output is correct
15 Correct 79 ms 13292 KB Output is correct
16 Correct 24 ms 8840 KB Output is correct
17 Correct 106 ms 15708 KB Output is correct
18 Correct 69 ms 11456 KB Output is correct
19 Correct 5 ms 4052 KB Output is correct
20 Correct 44 ms 10668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2680 KB Output is correct
9 Correct 2 ms 2680 KB Output is correct
10 Correct 2 ms 2684 KB Output is correct
11 Correct 2 ms 2676 KB Output is correct
12 Correct 2 ms 2676 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2680 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2680 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2684 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2676 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2680 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 1 ms 2644 KB Output is correct
33 Correct 2 ms 2644 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 2 ms 2644 KB Output is correct
40 Correct 1 ms 2680 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 2 ms 2680 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 2 ms 2644 KB Output is correct
47 Correct 1 ms 2656 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 2 ms 2644 KB Output is correct
50 Correct 2 ms 2644 KB Output is correct
51 Correct 2 ms 2644 KB Output is correct
52 Correct 2 ms 2676 KB Output is correct
53 Correct 2 ms 2644 KB Output is correct
54 Correct 2 ms 2644 KB Output is correct
55 Correct 2 ms 2644 KB Output is correct
56 Correct 2 ms 2684 KB Output is correct
57 Correct 2 ms 2644 KB Output is correct
58 Correct 2 ms 2644 KB Output is correct
59 Correct 2 ms 2644 KB Output is correct
60 Correct 2 ms 2644 KB Output is correct
61 Correct 2 ms 2644 KB Output is correct
62 Correct 2 ms 2644 KB Output is correct
63 Correct 2 ms 2644 KB Output is correct
64 Correct 2 ms 2644 KB Output is correct
65 Correct 2 ms 2644 KB Output is correct
66 Correct 2 ms 2680 KB Output is correct
67 Correct 2 ms 2644 KB Output is correct
68 Correct 2 ms 2644 KB Output is correct
69 Correct 2 ms 2644 KB Output is correct
70 Correct 2 ms 2644 KB Output is correct
71 Correct 2 ms 2772 KB Output is correct
72 Correct 2 ms 2644 KB Output is correct
73 Correct 2 ms 2644 KB Output is correct
74 Correct 2 ms 2644 KB Output is correct
75 Correct 2 ms 2680 KB Output is correct
76 Correct 2 ms 2656 KB Output is correct
77 Correct 2 ms 2772 KB Output is correct
78 Correct 2 ms 2644 KB Output is correct
79 Correct 2 ms 2644 KB Output is correct
80 Correct 2 ms 2772 KB Output is correct
81 Correct 2 ms 2772 KB Output is correct
82 Correct 2 ms 2772 KB Output is correct
83 Correct 2 ms 2772 KB Output is correct
84 Correct 2 ms 2772 KB Output is correct
85 Correct 2 ms 2644 KB Output is correct
86 Correct 2 ms 2680 KB Output is correct
87 Correct 2 ms 2900 KB Output is correct
88 Correct 2 ms 2644 KB Output is correct
89 Correct 2 ms 2644 KB Output is correct
90 Correct 3 ms 2772 KB Output is correct
91 Correct 2 ms 2644 KB Output is correct
92 Correct 2 ms 2644 KB Output is correct
93 Correct 2 ms 2644 KB Output is correct
94 Correct 2 ms 2680 KB Output is correct
95 Correct 2 ms 2644 KB Output is correct
96 Correct 2 ms 2772 KB Output is correct
97 Correct 2 ms 2772 KB Output is correct
98 Correct 2 ms 2772 KB Output is correct
99 Correct 20 ms 6104 KB Output is correct
100 Correct 3 ms 4948 KB Output is correct
101 Correct 20 ms 8068 KB Output is correct
102 Correct 25 ms 9812 KB Output is correct
103 Correct 91 ms 14664 KB Output is correct
104 Correct 77 ms 11788 KB Output is correct
105 Correct 92 ms 15044 KB Output is correct
106 Correct 5 ms 6996 KB Output is correct
107 Correct 4 ms 5460 KB Output is correct
108 Correct 101 ms 16112 KB Output is correct
109 Correct 83 ms 14160 KB Output is correct
110 Correct 81 ms 14464 KB Output is correct
111 Correct 2 ms 3444 KB Output is correct
112 Correct 2 ms 3412 KB Output is correct
113 Correct 79 ms 13292 KB Output is correct
114 Correct 24 ms 8840 KB Output is correct
115 Correct 106 ms 15708 KB Output is correct
116 Correct 69 ms 11456 KB Output is correct
117 Correct 5 ms 4052 KB Output is correct
118 Correct 44 ms 10668 KB Output is correct
119 Correct 3 ms 5204 KB Output is correct
120 Correct 74 ms 14456 KB Output is correct
121 Correct 46 ms 11468 KB Output is correct
122 Correct 2 ms 2644 KB Output is correct
123 Correct 5 ms 3984 KB Output is correct
124 Correct 22 ms 7140 KB Output is correct
125 Correct 23 ms 9696 KB Output is correct
126 Correct 4 ms 5844 KB Output is correct
127 Correct 10 ms 7704 KB Output is correct
128 Correct 74 ms 14412 KB Output is correct
129 Correct 41 ms 11660 KB Output is correct
130 Correct 49 ms 12476 KB Output is correct
131 Correct 3 ms 5204 KB Output is correct
132 Correct 3 ms 5332 KB Output is correct
133 Correct 91 ms 14760 KB Output is correct
134 Correct 21 ms 7856 KB Output is correct
135 Correct 3 ms 5332 KB Output is correct
136 Correct 42 ms 8336 KB Output is correct
137 Correct 63 ms 10956 KB Output is correct
138 Correct 6 ms 6868 KB Output is correct