Submission #574057

# Submission time Handle Problem Language Result Execution time Memory
574057 2022-06-07T16:22:06 Z MohammadAghil One-Way Streets (CEOI17_oneway) C++14
100 / 100
211 ms 26912 KB
      #include <bits/stdc++.h>
//   #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
    using namespace std;
  typedef long long ll;
   typedef pair<int, int> pp;
    #define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
      #define per(i,r,l) for(int i = (r); i >= (l); i--)
        #define rep(i,l,r) for(int i = (l); i < (r); i++)
           #define all(x) x.begin(), x.end()
              #define sz(x) (int)(x).size()
                  #define pb push_back
                      #define ss second
                           #define ff first
                                   void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 1e5 + 5, lg = 21, inf = ll(1e9) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;}
 
vector<int> adj[maxn];
int par[maxn], cnt[maxn];
vector<pp> his;
 
int get(int x){ return par[x] == -1? x: get(par[x]); }
bool unite(int u, int v){
     u = get(u), v = get(v);
     if(u == v) return his.pb({-1, -1}), false;
     if(cnt[u] < cnt[v]) swap(u, v);
     par[v] = u, cnt[u] += cnt[v];
     return his.pb({v, u}), true;
}
void undo(){
     auto[v, u] = his.back(); his.pop_back();
     if(u + 1) par[v] = -1, cnt[u] -= cnt[v];
}
 
pp edge[maxn];
bool cut_edge[maxn], vis[maxn];
 
void slv(int l, int r){
     if(l + 1 == r) {
          cut_edge[l] = get(edge[l].ff) != get(edge[l].ss);
          return;
     }
     int mid = (l + r)>>1;
     rep(i,mid,r) unite(edge[i].ff, edge[i].ss);
     slv(l, mid);
     rep(i,mid,r) undo();
     rep(i,l,mid) unite(edge[i].ff, edge[i].ss);
     slv(mid, r);
     rep(i,l,mid) undo();
}
 
vector<int> adj2[maxn];
int st[maxn];
map<pp, bool> is;
 
void add_query(int u, int v){
     st[u]++, st[v]--;
}
 
void dfs(int r, int p){
     vis[r] = true;
     for(int c: adj2[r]) if(c - p){
          dfs(c, r);
          st[r] += st[c];
     }
     if(st[r]){
          if(st[r] > 0) is[{r, p}] = true;
          else is[{p, r}] = true;
     }
}
 
int main(){
     cin.tie(0) -> sync_with_stdio(0);
     int n, m; cin >> n >> m;
     fill(par, par + n, -1);
     fill(cnt, cnt + n, 1);
     rep(i,0,m){
          cin >> edge[i].ff >> edge[i].ss; edge[i].ff--, edge[i].ss--;
     }
     slv(0, m);
     rep(i,0,m) if(!cut_edge[i]) unite(edge[i].ff, edge[i].ss);
     vector<int> grp(n, -1); int id = 0;
     rep(i,0,n){
          if(grp[get(i)] == -1) grp[get(i)] = id++;
          grp[i] = grp[get(i)];
     }
     rep(i,0,m) if(cut_edge[i]) {
          adj2[grp[edge[i].ff]].pb(grp[edge[i].ss]);
          adj2[grp[edge[i].ss]].pb(grp[edge[i].ff]);
     }
     int q; cin >> q;
     while(q--){
          int u, v; cin >> u >> v; u = grp[--u], v = grp[--v];
          add_query(u, v);
     }
     rep(i,0,n) if(!vis[i]) dfs(i, i);
     vector<int> ans(m);
     rep(i,0,m){
          if(cut_edge[i]){
               if(is.count({grp[edge[i].ff], grp[edge[i].ss]})) ans[i] = 1;
               else if(is.count({grp[edge[i].ss], grp[edge[i].ff]})) ans[i] = 2;
          }
     }
     for(int c: ans) cout << (c?(c&1?'R':'L'):'B');
     return 0; 
}

Compilation message

oneway.cpp: In function 'void undo()':
oneway.cpp:33:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   33 |      auto[v, u] = his.back(); his.pop_back();
      |          ^
oneway.cpp: In function 'int main()':
oneway.cpp:95:38: warning: operation on 'u' may be undefined [-Wsequence-point]
   95 |           int u, v; cin >> u >> v; u = grp[--u], v = grp[--v];
oneway.cpp:95:52: warning: operation on 'v' may be undefined [-Wsequence-point]
   95 |           int u, v; cin >> u >> v; u = grp[--u], v = grp[--v];
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5044 KB Output is correct
3 Correct 4 ms 5044 KB Output is correct
4 Correct 4 ms 5052 KB Output is correct
5 Correct 4 ms 5204 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 4 ms 5184 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 3 ms 5056 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5044 KB Output is correct
3 Correct 4 ms 5044 KB Output is correct
4 Correct 4 ms 5052 KB Output is correct
5 Correct 4 ms 5204 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 4 ms 5184 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 3 ms 5056 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 73 ms 8408 KB Output is correct
12 Correct 75 ms 8660 KB Output is correct
13 Correct 80 ms 9120 KB Output is correct
14 Correct 99 ms 10184 KB Output is correct
15 Correct 96 ms 10616 KB Output is correct
16 Correct 91 ms 13180 KB Output is correct
17 Correct 150 ms 18028 KB Output is correct
18 Correct 116 ms 13288 KB Output is correct
19 Correct 119 ms 19544 KB Output is correct
20 Correct 63 ms 8840 KB Output is correct
21 Correct 56 ms 9076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5044 KB Output is correct
3 Correct 4 ms 5044 KB Output is correct
4 Correct 4 ms 5052 KB Output is correct
5 Correct 4 ms 5204 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 4 ms 5184 KB Output is correct
8 Correct 4 ms 5076 KB Output is correct
9 Correct 3 ms 5056 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 73 ms 8408 KB Output is correct
12 Correct 75 ms 8660 KB Output is correct
13 Correct 80 ms 9120 KB Output is correct
14 Correct 99 ms 10184 KB Output is correct
15 Correct 96 ms 10616 KB Output is correct
16 Correct 91 ms 13180 KB Output is correct
17 Correct 150 ms 18028 KB Output is correct
18 Correct 116 ms 13288 KB Output is correct
19 Correct 119 ms 19544 KB Output is correct
20 Correct 63 ms 8840 KB Output is correct
21 Correct 56 ms 9076 KB Output is correct
22 Correct 211 ms 22544 KB Output is correct
23 Correct 173 ms 20360 KB Output is correct
24 Correct 173 ms 20112 KB Output is correct
25 Correct 181 ms 26912 KB Output is correct
26 Correct 175 ms 21940 KB Output is correct
27 Correct 163 ms 20296 KB Output is correct
28 Correct 73 ms 8228 KB Output is correct
29 Correct 79 ms 10056 KB Output is correct
30 Correct 83 ms 10088 KB Output is correct
31 Correct 76 ms 10156 KB Output is correct
32 Correct 91 ms 15844 KB Output is correct