Submission #1069337

# Submission time Handle Problem Language Result Execution time Memory
1069337 2024-08-21T19:42:58 Z Ahmed57 Simurgh (IOI17_simurgh) C++17
51 / 100
61 ms 15896 KB
#include "bits/stdc++.h"
#include "simurgh.h"

using namespace std;
#pragma GCC optimize("Ofast")
int vis[100001];
int dep[100001];
pair<int,int> mi[100001];
pair<int,int> PR[100001];
vector<pair<int,int>> adj[100001];
vector<pair<int,int>> back[100001];
vector<pair<int,int>> tree[100001];
vector<int> tr;
vector<pair<int,int>> edg;
void dfs(int i,int pr){
    vis[i] = 1;
    dep[i] = dep[pr]+1;
    mi[i] = {-1,-1};
    for(auto j:adj[i]){
        if(vis[j.first]&&dep[j.first]<dep[i]&&j.first!=pr){
            if(mi[i].first==-1)mi[i] = {i,j.first};
            else if(dep[j.first]<dep[mi[i].second])mi[i] = {i,j.first};
            back[i].push_back(j);
			//cout<<"back:"<<i<<" "<<j.first<<endl;
        }
		if(vis[j.first])continue;
        PR[j.first] = {i,j.second};
        tree[i].push_back(j);
		//cout<<"tree:"<<i<<" "<<j.first<<" "<<j.second<<endl;
        dfs(j.first,i);
        tr.push_back(j.second);
        if(mi[i].first==-1)mi[i] = mi[j.first];
        else if(mi[j.first].first!=-1&&dep[mi[j.first].second]<dep[mi[i].second])mi[i] = mi[j.first];
    }
}
int p[100001],N;
int ans[100001];
int find(int x){
    if(x==p[x])return x;
    return p[x] = find(p[x]);
}
void mergegroup(int a,int b){
    a = find(a);b = find(b);
    if(a==b)return ;
    p[a] = b;
}
int que(vector<int> v,bool ss){
    for(int i = 0;i<N;i++){
        p[i] = i;
    }
    for(int i = 0;i<v.size();i++){
        mergegroup(edg[v[i]].first,edg[v[i]].second);
    }
    int all = 0;
    for(auto i:tr){
        if(find(edg[i].first)!=find(edg[i].second)){
            v.push_back(i);
            all+=ans[i];
            mergegroup(edg[i].first,edg[i].second);
        }
    }
    if(ss)all = count_common_roads(v)-all;
    else all = count_common_roads(v);
    return all;
}
void answer(int i){
    for(auto j:tree[i]){
        if(ans[j.second]!=-1)continue;
		//cout<<j.second<<endl;
        int lol = mi[j.first].second;
        if(lol==-1||dep[lol]>dep[i]){
            ans[j.second] = 1;
        }else{
            int st = mi[j.first].first;
			//cout<<st<<" "<<lol<<endl;
            vector<int> v;
            while(st!=lol){
                v.push_back(PR[st].second);
                st = PR[st].first;
            }

            for(auto e:back[mi[j.first].first]){
                if(e.first==lol){
                    v.push_back(e.second);
                    break;
                }
            }
			//for(auto e:v)cout<<e<<"xx ";
			//cout<<endl;
            int ma = -1;
            for(auto e:v){
                if(ans[e]==0){
                    vector<int> Q;
                    for(int w:v){
                        if(w==e)continue;
                        Q.push_back(w);
                    }
                    ma = que(Q,0);
                    break;
                }
            }
			//cout<<"ddd"<<endl;
            if(ma!=-1){
				//cout<<j.second<<"lollll"<<endl;
                for(auto e:v){
                    if(ans[e]==-1){
                        vector<int> Q;
                        for(int w:v){
                            if(w==e)continue;
                            Q.push_back(w);
                        }
                        if(ma==que(Q,0)){
                            ans[e] = 0;
                        }else ans[e] = 1;
                    }
                }   
            }else{
                set<pair<int ,int >> s;
				//cout<<"dd"<<endl;
                for(auto e:v){
                    if(ans[e]==-1){
                        vector<int> Q;
                        for(int w:v){
                            if(w==e)continue;
                            Q.push_back(w);
                        }
                        s.insert({que(Q,0),e});
                    }
                }
                int la = (*(--s.end())).first;
                int sm = (*s.begin()).first;
                if(la==sm){
                    for(auto w:s)ans[w.second] = 0;
                }else{
                    for(auto w:s){
						//cout<<w.first<<"nanan"<<w.second<<endl;
                        if(w.first==la)ans[w.second] = 0;
                        else ans[w.second] = 1;
						//cout<<w.second<<" "<<ans[w.second]<<endl;
                    }
                }
            }
        }   
    }
	for(auto j:tree[i]){answer(j.first);}
}
vector<int> find_roads(int n, vector<int> u, vector<int> v){
	N = n;
	//cout<<"hhh"<<endl;
    for(int i = 0;i<u.size();i++){
        ans[i] = -1;
        adj[u[i]].push_back({v[i],i});
        adj[v[i]].push_back({u[i],i});
        edg.push_back({u[i],v[i]});
    }
    PR[0] = {-1,-1};
    dfs(0,0);
	//cout<<"hh"<<endl;
    answer(0);
	//cout<<"vvv"<<endl;
    for(int i = 0;i<n;i++){
        int la = 0;
        while(1){
            if(la==back[i].size())break;
            vector<int> Q;
            for(int e = la;e<back[i].size();e++)Q.push_back(back[i][e].second);
            if(que(Q,1)){
                int l = la , r = back[i].size()-1 , ANS = 0;
                while(l<=r){
                    int mid = (l+r)/2;
                    vector<int> Q;
                    for(int e = la;e<=mid;e++){
                        Q.push_back(back[i][e].second);
                    }
                    if(que(Q,1)){
                        ANS = mid;r = mid-1;
                    }else l = mid+1;
                }
                ans[back[i][ANS].second] = 1;
                la = ANS+1;
            }else break;
        }
    }
    vector<int> good;
    for(int i = 0;i<u.size();i++){
        if(ans[i]==1){
            good.push_back(i);
        }
    }
	//cout<<"ggg"<<endl;
	/*for(int i = 0;i<good.size();i++){
		cout<<good[i]<<"hhhh";
	}*/
    return good;
}

Compilation message

simurgh.cpp: In function 'int que(std::vector<int>, bool)':
simurgh.cpp:51:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for(int i = 0;i<v.size();i++){
      |                   ~^~~~~~~~~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:150:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  150 |     for(int i = 0;i<u.size();i++){
      |                   ~^~~~~~~~~
simurgh.cpp:164:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  164 |             if(la==back[i].size())break;
      |                ~~^~~~~~~~~~~~~~~~
simurgh.cpp:166:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  166 |             for(int e = la;e<back[i].size();e++)Q.push_back(back[i][e].second);
      |                            ~^~~~~~~~~~~~~~~
simurgh.cpp:185:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  185 |     for(int i = 0;i<u.size();i++){
      |                   ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8792 KB correct
2 Correct 2 ms 8540 KB correct
3 Correct 2 ms 8540 KB correct
4 Correct 1 ms 8540 KB correct
5 Correct 2 ms 8540 KB correct
6 Correct 1 ms 8540 KB correct
7 Correct 2 ms 8540 KB correct
8 Correct 2 ms 8792 KB correct
9 Correct 2 ms 8540 KB correct
10 Correct 2 ms 8540 KB correct
11 Correct 2 ms 8640 KB correct
12 Correct 2 ms 8536 KB correct
13 Correct 1 ms 8540 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8792 KB correct
2 Correct 2 ms 8540 KB correct
3 Correct 2 ms 8540 KB correct
4 Correct 1 ms 8540 KB correct
5 Correct 2 ms 8540 KB correct
6 Correct 1 ms 8540 KB correct
7 Correct 2 ms 8540 KB correct
8 Correct 2 ms 8792 KB correct
9 Correct 2 ms 8540 KB correct
10 Correct 2 ms 8540 KB correct
11 Correct 2 ms 8640 KB correct
12 Correct 2 ms 8536 KB correct
13 Correct 1 ms 8540 KB correct
14 Correct 2 ms 8540 KB correct
15 Correct 2 ms 8540 KB correct
16 Correct 2 ms 8540 KB correct
17 Correct 3 ms 8540 KB correct
18 Correct 3 ms 8540 KB correct
19 Correct 2 ms 8652 KB correct
20 Correct 3 ms 8536 KB correct
21 Correct 2 ms 8540 KB correct
22 Correct 2 ms 8540 KB correct
23 Correct 2 ms 8540 KB correct
24 Correct 2 ms 8540 KB correct
25 Correct 2 ms 8540 KB correct
26 Correct 2 ms 8540 KB correct
27 Correct 2 ms 8604 KB correct
28 Correct 2 ms 8540 KB correct
29 Correct 2 ms 8540 KB correct
30 Correct 2 ms 8540 KB correct
31 Correct 2 ms 8540 KB correct
32 Correct 2 ms 8536 KB correct
33 Correct 2 ms 8540 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8792 KB correct
2 Correct 2 ms 8540 KB correct
3 Correct 2 ms 8540 KB correct
4 Correct 1 ms 8540 KB correct
5 Correct 2 ms 8540 KB correct
6 Correct 1 ms 8540 KB correct
7 Correct 2 ms 8540 KB correct
8 Correct 2 ms 8792 KB correct
9 Correct 2 ms 8540 KB correct
10 Correct 2 ms 8540 KB correct
11 Correct 2 ms 8640 KB correct
12 Correct 2 ms 8536 KB correct
13 Correct 1 ms 8540 KB correct
14 Correct 2 ms 8540 KB correct
15 Correct 2 ms 8540 KB correct
16 Correct 2 ms 8540 KB correct
17 Correct 3 ms 8540 KB correct
18 Correct 3 ms 8540 KB correct
19 Correct 2 ms 8652 KB correct
20 Correct 3 ms 8536 KB correct
21 Correct 2 ms 8540 KB correct
22 Correct 2 ms 8540 KB correct
23 Correct 2 ms 8540 KB correct
24 Correct 2 ms 8540 KB correct
25 Correct 2 ms 8540 KB correct
26 Correct 2 ms 8540 KB correct
27 Correct 2 ms 8604 KB correct
28 Correct 2 ms 8540 KB correct
29 Correct 2 ms 8540 KB correct
30 Correct 2 ms 8540 KB correct
31 Correct 2 ms 8540 KB correct
32 Correct 2 ms 8536 KB correct
33 Correct 2 ms 8540 KB correct
34 Correct 23 ms 10452 KB correct
35 Correct 22 ms 10456 KB correct
36 Correct 19 ms 9944 KB correct
37 Correct 8 ms 8792 KB correct
38 Correct 24 ms 10452 KB correct
39 Correct 20 ms 10200 KB correct
40 Correct 18 ms 9940 KB correct
41 Correct 19 ms 10456 KB correct
42 Correct 22 ms 10580 KB correct
43 Correct 10 ms 9816 KB correct
44 Correct 11 ms 9564 KB correct
45 Correct 10 ms 9576 KB correct
46 Correct 11 ms 9308 KB correct
47 Correct 8 ms 9072 KB correct
48 Correct 4 ms 8540 KB correct
49 Correct 6 ms 8796 KB correct
50 Correct 7 ms 9048 KB correct
51 Correct 12 ms 9560 KB correct
52 Correct 10 ms 9560 KB correct
53 Correct 10 ms 9560 KB correct
54 Correct 13 ms 9820 KB correct
55 Correct 10 ms 9592 KB correct
56 Correct 11 ms 9560 KB correct
57 Correct 10 ms 9564 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB correct
2 Correct 2 ms 8540 KB correct
3 Correct 61 ms 13768 KB correct
4 Incorrect 21 ms 15896 KB WA in grader: NO
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8792 KB correct
2 Correct 2 ms 8540 KB correct
3 Correct 2 ms 8540 KB correct
4 Correct 1 ms 8540 KB correct
5 Correct 2 ms 8540 KB correct
6 Correct 1 ms 8540 KB correct
7 Correct 2 ms 8540 KB correct
8 Correct 2 ms 8792 KB correct
9 Correct 2 ms 8540 KB correct
10 Correct 2 ms 8540 KB correct
11 Correct 2 ms 8640 KB correct
12 Correct 2 ms 8536 KB correct
13 Correct 1 ms 8540 KB correct
14 Correct 2 ms 8540 KB correct
15 Correct 2 ms 8540 KB correct
16 Correct 2 ms 8540 KB correct
17 Correct 3 ms 8540 KB correct
18 Correct 3 ms 8540 KB correct
19 Correct 2 ms 8652 KB correct
20 Correct 3 ms 8536 KB correct
21 Correct 2 ms 8540 KB correct
22 Correct 2 ms 8540 KB correct
23 Correct 2 ms 8540 KB correct
24 Correct 2 ms 8540 KB correct
25 Correct 2 ms 8540 KB correct
26 Correct 2 ms 8540 KB correct
27 Correct 2 ms 8604 KB correct
28 Correct 2 ms 8540 KB correct
29 Correct 2 ms 8540 KB correct
30 Correct 2 ms 8540 KB correct
31 Correct 2 ms 8540 KB correct
32 Correct 2 ms 8536 KB correct
33 Correct 2 ms 8540 KB correct
34 Correct 23 ms 10452 KB correct
35 Correct 22 ms 10456 KB correct
36 Correct 19 ms 9944 KB correct
37 Correct 8 ms 8792 KB correct
38 Correct 24 ms 10452 KB correct
39 Correct 20 ms 10200 KB correct
40 Correct 18 ms 9940 KB correct
41 Correct 19 ms 10456 KB correct
42 Correct 22 ms 10580 KB correct
43 Correct 10 ms 9816 KB correct
44 Correct 11 ms 9564 KB correct
45 Correct 10 ms 9576 KB correct
46 Correct 11 ms 9308 KB correct
47 Correct 8 ms 9072 KB correct
48 Correct 4 ms 8540 KB correct
49 Correct 6 ms 8796 KB correct
50 Correct 7 ms 9048 KB correct
51 Correct 12 ms 9560 KB correct
52 Correct 10 ms 9560 KB correct
53 Correct 10 ms 9560 KB correct
54 Correct 13 ms 9820 KB correct
55 Correct 10 ms 9592 KB correct
56 Correct 11 ms 9560 KB correct
57 Correct 10 ms 9564 KB correct
58 Correct 1 ms 8536 KB correct
59 Correct 2 ms 8540 KB correct
60 Correct 61 ms 13768 KB correct
61 Incorrect 21 ms 15896 KB WA in grader: NO
62 Halted 0 ms 0 KB -