Submission #591735

# Submission time Handle Problem Language Result Execution time Memory
591735 2022-07-07T19:59:56 Z NintsiChkhaidze Graph (BOI20_graph) C++14
100 / 100
145 ms 20932 KB
#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
using namespace std;

const int N = 100005;
vector <pair<int,int> > v[N];
pair <int,double> d[N];
vector <int> pr;
int vis[N],q;
double val;
int p[N];

void dfs(int x){
    pr.pb(x);
    vis[x] = 1;
    for (int i = 0; i < v[x].size(); i++){
        int to = v[x][i].f,col = v[x][i].s,av = -d[x].f;
        double bv = 1.0 - d[x].s;
        if (col == 2) bv+=1.0;

        if (!vis[to]) {
            d[to] = {av,bv};
            p[to] = x;
            dfs(to);
        }else if (av == d[to].f && bv != d[to].s) {
            cout<<"NO";
            exit(0);
        }else if (av == d[to].f && bv == d[to].s){
            continue;
        }else{
            q=1;
            val = (d[to].s - bv)/(av - d[to].f);
        }
    }
}
signed main() {
    ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
    int n,m;
    cin>>n>>m;

    for (int i = 1; i <= m; i++){
        int a,b,c;
        cin>>a>>b>>c;
        v[a].pb({b,c});
        v[b].pb({a,c});
    }

    for (int i = 1; i <= n; i++){
        if (vis[i]) continue;
        pr.clear(); q=0;
        d[i] = {1,0.0};
        dfs(i);
        if (!q){
            vector <double> vec; vec.clear();
            for (int x: pr)
                vec.pb(-d[x].f*d[x].s);
            sort(vec.begin(),vec.end());
            val = vec[(vec.size())/2];
        }
        for (int x: pr)
            d[x] = {0,val*d[x].f + d[x].s};

        bool check = 1;
        for (int x: pr)
            for (int j = 0; j < v[x].size(); j++){
                int to = v[x][j].f,col = v[x][j].s;
                if (d[x].s + d[to].s != col) check=0;
            }

        if (!check) {
            cout<<"NO";
            exit(0);
        }
    }
    cout<<"YES"<<endl;
    for (int i =1; i <= n;i++)
        cout<<d[i].s<<" ";
}

Compilation message

Graph.cpp: In function 'void dfs(int)':
Graph.cpp:18:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |     for (int i = 0; i < v[x].size(); i++){
      |                     ~~^~~~~~~~~~~~~
Graph.cpp: In function 'int main()':
Graph.cpp:67:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |             for (int j = 0; j < v[x].size(); j++){
      |                             ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 1 ms 2688 KB answer = YES
4 Correct 2 ms 2772 KB answer = NO
5 Correct 2 ms 2644 KB answer = YES
6 Correct 2 ms 2644 KB answer = YES
7 Correct 1 ms 2644 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 2 ms 2688 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 1 ms 2684 KB answer = NO
13 Correct 2 ms 2688 KB answer = YES
14 Correct 2 ms 2688 KB answer = YES
15 Correct 2 ms 2684 KB answer = YES
16 Correct 1 ms 2644 KB answer = YES
17 Correct 1 ms 2644 KB answer = YES
18 Correct 1 ms 2644 KB answer = YES
19 Correct 1 ms 2644 KB answer = YES
20 Correct 1 ms 2644 KB answer = YES
21 Correct 1 ms 2688 KB answer = YES
22 Correct 2 ms 2688 KB answer = NO
23 Correct 1 ms 2688 KB answer = NO
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 1 ms 2688 KB answer = YES
4 Correct 2 ms 2772 KB answer = NO
5 Correct 2 ms 2644 KB answer = YES
6 Correct 2 ms 2644 KB answer = YES
7 Correct 1 ms 2644 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 2 ms 2688 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 1 ms 2684 KB answer = NO
13 Correct 2 ms 2688 KB answer = YES
14 Correct 2 ms 2688 KB answer = YES
15 Correct 2 ms 2684 KB answer = YES
16 Correct 1 ms 2644 KB answer = YES
17 Correct 1 ms 2644 KB answer = YES
18 Correct 1 ms 2644 KB answer = YES
19 Correct 1 ms 2644 KB answer = YES
20 Correct 1 ms 2644 KB answer = YES
21 Correct 1 ms 2688 KB answer = YES
22 Correct 2 ms 2688 KB answer = NO
23 Correct 1 ms 2688 KB answer = NO
24 Correct 2 ms 2772 KB answer = YES
25 Correct 2 ms 2644 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 2 ms 2644 KB answer = YES
28 Correct 2 ms 2744 KB answer = YES
29 Correct 1 ms 2644 KB answer = YES
30 Correct 2 ms 2644 KB answer = NO
31 Correct 2 ms 2644 KB answer = YES
32 Correct 2 ms 2688 KB answer = YES
33 Correct 2 ms 2680 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 2 ms 2688 KB answer = YES
36 Correct 2 ms 2644 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 1 ms 2688 KB answer = YES
4 Correct 2 ms 2772 KB answer = NO
5 Correct 2 ms 2644 KB answer = YES
6 Correct 2 ms 2644 KB answer = YES
7 Correct 1 ms 2644 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 2 ms 2688 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 1 ms 2684 KB answer = NO
13 Correct 2 ms 2688 KB answer = YES
14 Correct 2 ms 2688 KB answer = YES
15 Correct 2 ms 2684 KB answer = YES
16 Correct 1 ms 2644 KB answer = YES
17 Correct 1 ms 2644 KB answer = YES
18 Correct 1 ms 2644 KB answer = YES
19 Correct 1 ms 2644 KB answer = YES
20 Correct 1 ms 2644 KB answer = YES
21 Correct 1 ms 2688 KB answer = YES
22 Correct 2 ms 2688 KB answer = NO
23 Correct 1 ms 2688 KB answer = NO
24 Correct 2 ms 2772 KB answer = YES
25 Correct 2 ms 2644 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 2 ms 2644 KB answer = YES
28 Correct 2 ms 2744 KB answer = YES
29 Correct 1 ms 2644 KB answer = YES
30 Correct 2 ms 2644 KB answer = NO
31 Correct 2 ms 2644 KB answer = YES
32 Correct 2 ms 2688 KB answer = YES
33 Correct 2 ms 2680 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 2 ms 2688 KB answer = YES
36 Correct 2 ms 2644 KB answer = YES
37 Correct 2 ms 2644 KB answer = YES
38 Correct 2 ms 2644 KB answer = YES
39 Correct 2 ms 2644 KB answer = YES
40 Correct 2 ms 2772 KB answer = YES
41 Correct 2 ms 2772 KB answer = NO
42 Correct 2 ms 2772 KB answer = YES
43 Correct 2 ms 2688 KB answer = YES
44 Correct 3 ms 2772 KB answer = YES
45 Correct 3 ms 2772 KB answer = YES
46 Correct 2 ms 2692 KB answer = YES
47 Correct 2 ms 2772 KB answer = YES
48 Correct 2 ms 2772 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 1 ms 2688 KB answer = YES
4 Correct 2 ms 2772 KB answer = NO
5 Correct 2 ms 2644 KB answer = YES
6 Correct 2 ms 2644 KB answer = YES
7 Correct 1 ms 2644 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 2 ms 2688 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 1 ms 2684 KB answer = NO
13 Correct 2 ms 2688 KB answer = YES
14 Correct 2 ms 2688 KB answer = YES
15 Correct 2 ms 2684 KB answer = YES
16 Correct 1 ms 2644 KB answer = YES
17 Correct 1 ms 2644 KB answer = YES
18 Correct 1 ms 2644 KB answer = YES
19 Correct 1 ms 2644 KB answer = YES
20 Correct 1 ms 2644 KB answer = YES
21 Correct 1 ms 2688 KB answer = YES
22 Correct 2 ms 2688 KB answer = NO
23 Correct 1 ms 2688 KB answer = NO
24 Correct 2 ms 2772 KB answer = YES
25 Correct 2 ms 2644 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 2 ms 2644 KB answer = YES
28 Correct 2 ms 2744 KB answer = YES
29 Correct 1 ms 2644 KB answer = YES
30 Correct 2 ms 2644 KB answer = NO
31 Correct 2 ms 2644 KB answer = YES
32 Correct 2 ms 2688 KB answer = YES
33 Correct 2 ms 2680 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 2 ms 2688 KB answer = YES
36 Correct 2 ms 2644 KB answer = YES
37 Correct 2 ms 2644 KB answer = YES
38 Correct 2 ms 2644 KB answer = YES
39 Correct 2 ms 2644 KB answer = YES
40 Correct 2 ms 2772 KB answer = YES
41 Correct 2 ms 2772 KB answer = NO
42 Correct 2 ms 2772 KB answer = YES
43 Correct 2 ms 2688 KB answer = YES
44 Correct 3 ms 2772 KB answer = YES
45 Correct 3 ms 2772 KB answer = YES
46 Correct 2 ms 2692 KB answer = YES
47 Correct 2 ms 2772 KB answer = YES
48 Correct 2 ms 2772 KB answer = YES
49 Correct 9 ms 3668 KB answer = YES
50 Correct 10 ms 3828 KB answer = YES
51 Correct 10 ms 3900 KB answer = YES
52 Correct 5 ms 3284 KB answer = NO
53 Correct 3 ms 2692 KB answer = YES
54 Correct 3 ms 2900 KB answer = YES
55 Correct 7 ms 3216 KB answer = YES
56 Correct 9 ms 3648 KB answer = YES
57 Correct 8 ms 3412 KB answer = YES
58 Correct 9 ms 3412 KB answer = YES
59 Correct 10 ms 3336 KB answer = YES
60 Correct 8 ms 3540 KB answer = YES
61 Correct 6 ms 3028 KB answer = YES
62 Correct 49 ms 9820 KB answer = NO
63 Correct 55 ms 10312 KB answer = YES
64 Correct 54 ms 10248 KB answer = NO
65 Correct 58 ms 10324 KB answer = YES
66 Correct 3 ms 2772 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 1 ms 2688 KB answer = YES
4 Correct 2 ms 2772 KB answer = NO
5 Correct 2 ms 2644 KB answer = YES
6 Correct 2 ms 2644 KB answer = YES
7 Correct 1 ms 2644 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 2 ms 2688 KB answer = NO
10 Correct 2 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 1 ms 2684 KB answer = NO
13 Correct 2 ms 2688 KB answer = YES
14 Correct 2 ms 2688 KB answer = YES
15 Correct 2 ms 2684 KB answer = YES
16 Correct 1 ms 2644 KB answer = YES
17 Correct 1 ms 2644 KB answer = YES
18 Correct 1 ms 2644 KB answer = YES
19 Correct 1 ms 2644 KB answer = YES
20 Correct 1 ms 2644 KB answer = YES
21 Correct 1 ms 2688 KB answer = YES
22 Correct 2 ms 2688 KB answer = NO
23 Correct 1 ms 2688 KB answer = NO
24 Correct 2 ms 2772 KB answer = YES
25 Correct 2 ms 2644 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 2 ms 2644 KB answer = YES
28 Correct 2 ms 2744 KB answer = YES
29 Correct 1 ms 2644 KB answer = YES
30 Correct 2 ms 2644 KB answer = NO
31 Correct 2 ms 2644 KB answer = YES
32 Correct 2 ms 2688 KB answer = YES
33 Correct 2 ms 2680 KB answer = YES
34 Correct 2 ms 2644 KB answer = YES
35 Correct 2 ms 2688 KB answer = YES
36 Correct 2 ms 2644 KB answer = YES
37 Correct 2 ms 2644 KB answer = YES
38 Correct 2 ms 2644 KB answer = YES
39 Correct 2 ms 2644 KB answer = YES
40 Correct 2 ms 2772 KB answer = YES
41 Correct 2 ms 2772 KB answer = NO
42 Correct 2 ms 2772 KB answer = YES
43 Correct 2 ms 2688 KB answer = YES
44 Correct 3 ms 2772 KB answer = YES
45 Correct 3 ms 2772 KB answer = YES
46 Correct 2 ms 2692 KB answer = YES
47 Correct 2 ms 2772 KB answer = YES
48 Correct 2 ms 2772 KB answer = YES
49 Correct 9 ms 3668 KB answer = YES
50 Correct 10 ms 3828 KB answer = YES
51 Correct 10 ms 3900 KB answer = YES
52 Correct 5 ms 3284 KB answer = NO
53 Correct 3 ms 2692 KB answer = YES
54 Correct 3 ms 2900 KB answer = YES
55 Correct 7 ms 3216 KB answer = YES
56 Correct 9 ms 3648 KB answer = YES
57 Correct 8 ms 3412 KB answer = YES
58 Correct 9 ms 3412 KB answer = YES
59 Correct 10 ms 3336 KB answer = YES
60 Correct 8 ms 3540 KB answer = YES
61 Correct 6 ms 3028 KB answer = YES
62 Correct 49 ms 9820 KB answer = NO
63 Correct 55 ms 10312 KB answer = YES
64 Correct 54 ms 10248 KB answer = NO
65 Correct 58 ms 10324 KB answer = YES
66 Correct 3 ms 2772 KB answer = YES
67 Correct 81 ms 17276 KB answer = YES
68 Correct 72 ms 17352 KB answer = YES
69 Correct 79 ms 16376 KB answer = YES
70 Correct 105 ms 20932 KB answer = YES
71 Correct 75 ms 16608 KB answer = YES
72 Correct 106 ms 11724 KB answer = YES
73 Correct 84 ms 10788 KB answer = YES
74 Correct 52 ms 10832 KB answer = YES
75 Correct 24 ms 8952 KB answer = NO
76 Correct 11 ms 3796 KB answer = YES
77 Correct 21 ms 4948 KB answer = YES
78 Correct 34 ms 6612 KB answer = YES
79 Correct 73 ms 10504 KB answer = YES
80 Correct 60 ms 11212 KB answer = YES
81 Correct 39 ms 13208 KB answer = NO
82 Correct 87 ms 16172 KB answer = YES
83 Correct 105 ms 16632 KB answer = YES
84 Correct 102 ms 17236 KB answer = YES
85 Correct 76 ms 17232 KB answer = YES
86 Correct 79 ms 16528 KB answer = YES
87 Correct 50 ms 11508 KB answer = NO
88 Correct 99 ms 12852 KB answer = YES
89 Correct 78 ms 9748 KB answer = YES
90 Correct 82 ms 9740 KB answer = YES
91 Correct 82 ms 9764 KB answer = YES
92 Correct 45 ms 6920 KB answer = YES
93 Correct 40 ms 6744 KB answer = YES
94 Correct 67 ms 16252 KB answer = NO
95 Correct 40 ms 9488 KB answer = NO
96 Correct 145 ms 18560 KB answer = YES
97 Correct 34 ms 16244 KB answer = NO