Submission #581675

# Submission time Handle Problem Language Result Execution time Memory
581675 2022-06-23T02:54:30 Z elpro123 Graph (BOI20_graph) C++14
100 / 100
315 ms 27184 KB
#include <bits/stdc++.h>

using namespace std;
 
/*
- Los valores de los distintos componentes conectados son independientes entre sí.
- Cuando se hace un DFS de un CC(componente conectado), el vértice inicial tiene un valor de x
- Para cada eje (a, b, c)
    Si b no está explorado, valor[b] = c - valor[a]
    Si b está explorado, pero la expresion c = valor[a] + valor[b] no da 
    ninguna información adicional, no se hace nada
    Si b está explorado, y la expresión da información adicional, se usa para calcular x.
    para evitar WA verificar el resultado
*/
 

vector<int> eje[100001];
vector<int> color[100001];
vector<double> coefx(100001, 0.0);
vector<double> coefc(100001, 0.0);
vector<int> componentes(100001, 0);
vector<double> xval(1);
vector<double> findx;
int comp_cnt = 0;
 
void dfs(int u){
    findx.push_back(coefc[u] * coefx[u] * -1.0);
    for(int i = 0; i < eje[u].size(); i++){
        int v = eje[u][i];
        int c = color[u][i];
 
        if(!componentes[v]){
            componentes[v] = comp_cnt;
            coefx[v] = -1.0 * coefx[u];
            coefc[v] = double(c) - coefc[u];
            dfs(v);
        }else{
            if(coefx[u] + coefx[v] == 0.0) continue;
            xval.back() = (double(c) - (coefc[u] + coefc[v]))/(coefx[u] + coefx[v]);
        }
    }
}
 
int main(){
    int n, m;
    cin>>n>>m;
 
    int a, b, c;
    for(int i = 1; i <= m; i++){
        cin>>a>>b>>c;
 
        eje[a].push_back(b);
        color[a].push_back(c);
 
        eje[b].push_back(a);
        color[b].push_back(c);
    }
 
    for(int i = 1; i <= n; i++){
        if(componentes[i]) continue;
        comp_cnt++;
        componentes[i] = comp_cnt;
        coefx[i] = 1.0;
        xval.push_back(-9999.9);
        findx.clear();
        dfs(i);
        if(xval.back() == -9999.9){
            sort(findx.begin(), findx.end());
            xval.back() = findx[findx.size()/2];
        }
    }
 
    double val[n+1];
    for(int i = 1; i <= n; i++){ 
        val[i] = coefx[i] * xval[componentes[i]] + coefc[i];
    }
    for(int i = 1; i <= n; i++){
        for(int j = 0; j < eje[i].size(); j++){
            if(val[i] + val[eje[i][j]] != color[i][j]){
                cout<<"NO\n";
                return 0;
            }
        }
    }
    cout<<"YES\n";
    for(int i = 1; i <= n; i++){
        cout << val[i] << ' ';
    }
    cout << '\n';
}

Compilation message

Graph.cpp: In function 'void dfs(int)':
Graph.cpp:28:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |     for(int i = 0; i < eje[u].size(); i++){
      |                    ~~^~~~~~~~~~~~~~~
Graph.cpp: In function 'int main()':
Graph.cpp:78:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |         for(int j = 0; j < eje[i].size(); j++){
      |                        ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6868 KB answer = YES
2 Correct 4 ms 6868 KB answer = YES
3 Correct 4 ms 6972 KB answer = YES
4 Correct 4 ms 6868 KB answer = NO
5 Correct 4 ms 7104 KB answer = YES
6 Correct 4 ms 6968 KB answer = YES
7 Correct 4 ms 6984 KB answer = YES
8 Correct 4 ms 6868 KB answer = YES
9 Correct 4 ms 6868 KB answer = NO
10 Correct 4 ms 6972 KB answer = YES
11 Correct 4 ms 6976 KB answer = YES
12 Correct 4 ms 6868 KB answer = NO
13 Correct 4 ms 6868 KB answer = YES
14 Correct 4 ms 6976 KB answer = YES
15 Correct 5 ms 6972 KB answer = YES
16 Correct 4 ms 6976 KB answer = YES
17 Correct 4 ms 6868 KB answer = YES
18 Correct 4 ms 6868 KB answer = YES
19 Correct 3 ms 6972 KB answer = YES
20 Correct 4 ms 6972 KB answer = YES
21 Correct 4 ms 6868 KB answer = YES
22 Correct 4 ms 6956 KB answer = NO
23 Correct 4 ms 6980 KB answer = NO
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6868 KB answer = YES
2 Correct 4 ms 6868 KB answer = YES
3 Correct 4 ms 6972 KB answer = YES
4 Correct 4 ms 6868 KB answer = NO
5 Correct 4 ms 7104 KB answer = YES
6 Correct 4 ms 6968 KB answer = YES
7 Correct 4 ms 6984 KB answer = YES
8 Correct 4 ms 6868 KB answer = YES
9 Correct 4 ms 6868 KB answer = NO
10 Correct 4 ms 6972 KB answer = YES
11 Correct 4 ms 6976 KB answer = YES
12 Correct 4 ms 6868 KB answer = NO
13 Correct 4 ms 6868 KB answer = YES
14 Correct 4 ms 6976 KB answer = YES
15 Correct 5 ms 6972 KB answer = YES
16 Correct 4 ms 6976 KB answer = YES
17 Correct 4 ms 6868 KB answer = YES
18 Correct 4 ms 6868 KB answer = YES
19 Correct 3 ms 6972 KB answer = YES
20 Correct 4 ms 6972 KB answer = YES
21 Correct 4 ms 6868 KB answer = YES
22 Correct 4 ms 6956 KB answer = NO
23 Correct 4 ms 6980 KB answer = NO
24 Correct 4 ms 6868 KB answer = YES
25 Correct 4 ms 6868 KB answer = YES
26 Correct 4 ms 6868 KB answer = YES
27 Correct 4 ms 6976 KB answer = YES
28 Correct 5 ms 6976 KB answer = YES
29 Correct 4 ms 6868 KB answer = YES
30 Correct 4 ms 6868 KB answer = NO
31 Correct 4 ms 6972 KB answer = YES
32 Correct 4 ms 6980 KB answer = YES
33 Correct 4 ms 6972 KB answer = YES
34 Correct 4 ms 6868 KB answer = YES
35 Correct 4 ms 6868 KB answer = YES
36 Correct 4 ms 6868 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6868 KB answer = YES
2 Correct 4 ms 6868 KB answer = YES
3 Correct 4 ms 6972 KB answer = YES
4 Correct 4 ms 6868 KB answer = NO
5 Correct 4 ms 7104 KB answer = YES
6 Correct 4 ms 6968 KB answer = YES
7 Correct 4 ms 6984 KB answer = YES
8 Correct 4 ms 6868 KB answer = YES
9 Correct 4 ms 6868 KB answer = NO
10 Correct 4 ms 6972 KB answer = YES
11 Correct 4 ms 6976 KB answer = YES
12 Correct 4 ms 6868 KB answer = NO
13 Correct 4 ms 6868 KB answer = YES
14 Correct 4 ms 6976 KB answer = YES
15 Correct 5 ms 6972 KB answer = YES
16 Correct 4 ms 6976 KB answer = YES
17 Correct 4 ms 6868 KB answer = YES
18 Correct 4 ms 6868 KB answer = YES
19 Correct 3 ms 6972 KB answer = YES
20 Correct 4 ms 6972 KB answer = YES
21 Correct 4 ms 6868 KB answer = YES
22 Correct 4 ms 6956 KB answer = NO
23 Correct 4 ms 6980 KB answer = NO
24 Correct 4 ms 6868 KB answer = YES
25 Correct 4 ms 6868 KB answer = YES
26 Correct 4 ms 6868 KB answer = YES
27 Correct 4 ms 6976 KB answer = YES
28 Correct 5 ms 6976 KB answer = YES
29 Correct 4 ms 6868 KB answer = YES
30 Correct 4 ms 6868 KB answer = NO
31 Correct 4 ms 6972 KB answer = YES
32 Correct 4 ms 6980 KB answer = YES
33 Correct 4 ms 6972 KB answer = YES
34 Correct 4 ms 6868 KB answer = YES
35 Correct 4 ms 6868 KB answer = YES
36 Correct 4 ms 6868 KB answer = YES
37 Correct 4 ms 6868 KB answer = YES
38 Correct 4 ms 6868 KB answer = YES
39 Correct 5 ms 6996 KB answer = YES
40 Correct 5 ms 6996 KB answer = YES
41 Correct 5 ms 6988 KB answer = NO
42 Correct 5 ms 6996 KB answer = YES
43 Correct 5 ms 6984 KB answer = YES
44 Correct 5 ms 6996 KB answer = YES
45 Correct 6 ms 6984 KB answer = YES
46 Correct 4 ms 6996 KB answer = YES
47 Correct 5 ms 6984 KB answer = YES
48 Correct 5 ms 6996 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6868 KB answer = YES
2 Correct 4 ms 6868 KB answer = YES
3 Correct 4 ms 6972 KB answer = YES
4 Correct 4 ms 6868 KB answer = NO
5 Correct 4 ms 7104 KB answer = YES
6 Correct 4 ms 6968 KB answer = YES
7 Correct 4 ms 6984 KB answer = YES
8 Correct 4 ms 6868 KB answer = YES
9 Correct 4 ms 6868 KB answer = NO
10 Correct 4 ms 6972 KB answer = YES
11 Correct 4 ms 6976 KB answer = YES
12 Correct 4 ms 6868 KB answer = NO
13 Correct 4 ms 6868 KB answer = YES
14 Correct 4 ms 6976 KB answer = YES
15 Correct 5 ms 6972 KB answer = YES
16 Correct 4 ms 6976 KB answer = YES
17 Correct 4 ms 6868 KB answer = YES
18 Correct 4 ms 6868 KB answer = YES
19 Correct 3 ms 6972 KB answer = YES
20 Correct 4 ms 6972 KB answer = YES
21 Correct 4 ms 6868 KB answer = YES
22 Correct 4 ms 6956 KB answer = NO
23 Correct 4 ms 6980 KB answer = NO
24 Correct 4 ms 6868 KB answer = YES
25 Correct 4 ms 6868 KB answer = YES
26 Correct 4 ms 6868 KB answer = YES
27 Correct 4 ms 6976 KB answer = YES
28 Correct 5 ms 6976 KB answer = YES
29 Correct 4 ms 6868 KB answer = YES
30 Correct 4 ms 6868 KB answer = NO
31 Correct 4 ms 6972 KB answer = YES
32 Correct 4 ms 6980 KB answer = YES
33 Correct 4 ms 6972 KB answer = YES
34 Correct 4 ms 6868 KB answer = YES
35 Correct 4 ms 6868 KB answer = YES
36 Correct 4 ms 6868 KB answer = YES
37 Correct 4 ms 6868 KB answer = YES
38 Correct 4 ms 6868 KB answer = YES
39 Correct 5 ms 6996 KB answer = YES
40 Correct 5 ms 6996 KB answer = YES
41 Correct 5 ms 6988 KB answer = NO
42 Correct 5 ms 6996 KB answer = YES
43 Correct 5 ms 6984 KB answer = YES
44 Correct 5 ms 6996 KB answer = YES
45 Correct 6 ms 6984 KB answer = YES
46 Correct 4 ms 6996 KB answer = YES
47 Correct 5 ms 6984 KB answer = YES
48 Correct 5 ms 6996 KB answer = YES
49 Correct 16 ms 8020 KB answer = YES
50 Correct 17 ms 8276 KB answer = YES
51 Correct 16 ms 8304 KB answer = YES
52 Correct 14 ms 8268 KB answer = NO
53 Correct 6 ms 7048 KB answer = YES
54 Correct 7 ms 7112 KB answer = YES
55 Correct 10 ms 7496 KB answer = YES
56 Correct 16 ms 7928 KB answer = YES
57 Correct 16 ms 7792 KB answer = YES
58 Correct 15 ms 7764 KB answer = YES
59 Correct 15 ms 7764 KB answer = YES
60 Correct 18 ms 7880 KB answer = YES
61 Correct 11 ms 7468 KB answer = YES
62 Correct 163 ms 14624 KB answer = NO
63 Correct 153 ms 14628 KB answer = YES
64 Correct 155 ms 14616 KB answer = NO
65 Correct 152 ms 14540 KB answer = YES
66 Correct 6 ms 7124 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6868 KB answer = YES
2 Correct 4 ms 6868 KB answer = YES
3 Correct 4 ms 6972 KB answer = YES
4 Correct 4 ms 6868 KB answer = NO
5 Correct 4 ms 7104 KB answer = YES
6 Correct 4 ms 6968 KB answer = YES
7 Correct 4 ms 6984 KB answer = YES
8 Correct 4 ms 6868 KB answer = YES
9 Correct 4 ms 6868 KB answer = NO
10 Correct 4 ms 6972 KB answer = YES
11 Correct 4 ms 6976 KB answer = YES
12 Correct 4 ms 6868 KB answer = NO
13 Correct 4 ms 6868 KB answer = YES
14 Correct 4 ms 6976 KB answer = YES
15 Correct 5 ms 6972 KB answer = YES
16 Correct 4 ms 6976 KB answer = YES
17 Correct 4 ms 6868 KB answer = YES
18 Correct 4 ms 6868 KB answer = YES
19 Correct 3 ms 6972 KB answer = YES
20 Correct 4 ms 6972 KB answer = YES
21 Correct 4 ms 6868 KB answer = YES
22 Correct 4 ms 6956 KB answer = NO
23 Correct 4 ms 6980 KB answer = NO
24 Correct 4 ms 6868 KB answer = YES
25 Correct 4 ms 6868 KB answer = YES
26 Correct 4 ms 6868 KB answer = YES
27 Correct 4 ms 6976 KB answer = YES
28 Correct 5 ms 6976 KB answer = YES
29 Correct 4 ms 6868 KB answer = YES
30 Correct 4 ms 6868 KB answer = NO
31 Correct 4 ms 6972 KB answer = YES
32 Correct 4 ms 6980 KB answer = YES
33 Correct 4 ms 6972 KB answer = YES
34 Correct 4 ms 6868 KB answer = YES
35 Correct 4 ms 6868 KB answer = YES
36 Correct 4 ms 6868 KB answer = YES
37 Correct 4 ms 6868 KB answer = YES
38 Correct 4 ms 6868 KB answer = YES
39 Correct 5 ms 6996 KB answer = YES
40 Correct 5 ms 6996 KB answer = YES
41 Correct 5 ms 6988 KB answer = NO
42 Correct 5 ms 6996 KB answer = YES
43 Correct 5 ms 6984 KB answer = YES
44 Correct 5 ms 6996 KB answer = YES
45 Correct 6 ms 6984 KB answer = YES
46 Correct 4 ms 6996 KB answer = YES
47 Correct 5 ms 6984 KB answer = YES
48 Correct 5 ms 6996 KB answer = YES
49 Correct 16 ms 8020 KB answer = YES
50 Correct 17 ms 8276 KB answer = YES
51 Correct 16 ms 8304 KB answer = YES
52 Correct 14 ms 8268 KB answer = NO
53 Correct 6 ms 7048 KB answer = YES
54 Correct 7 ms 7112 KB answer = YES
55 Correct 10 ms 7496 KB answer = YES
56 Correct 16 ms 7928 KB answer = YES
57 Correct 16 ms 7792 KB answer = YES
58 Correct 15 ms 7764 KB answer = YES
59 Correct 15 ms 7764 KB answer = YES
60 Correct 18 ms 7880 KB answer = YES
61 Correct 11 ms 7468 KB answer = YES
62 Correct 163 ms 14624 KB answer = NO
63 Correct 153 ms 14628 KB answer = YES
64 Correct 155 ms 14616 KB answer = NO
65 Correct 152 ms 14540 KB answer = YES
66 Correct 6 ms 7124 KB answer = YES
67 Correct 134 ms 23552 KB answer = YES
68 Correct 136 ms 23416 KB answer = YES
69 Correct 128 ms 23476 KB answer = YES
70 Correct 214 ms 27184 KB answer = YES
71 Correct 134 ms 23696 KB answer = YES
72 Correct 179 ms 16588 KB answer = YES
73 Correct 179 ms 16676 KB answer = YES
74 Correct 102 ms 16764 KB answer = YES
75 Correct 77 ms 16540 KB answer = NO
76 Correct 20 ms 8204 KB answer = YES
77 Correct 38 ms 9428 KB answer = YES
78 Correct 68 ms 11088 KB answer = YES
79 Correct 147 ms 15224 KB answer = YES
80 Correct 107 ms 16740 KB answer = YES
81 Correct 128 ms 19460 KB answer = NO
82 Correct 169 ms 23140 KB answer = YES
83 Correct 184 ms 23644 KB answer = YES
84 Correct 190 ms 23608 KB answer = YES
85 Correct 138 ms 23508 KB answer = YES
86 Correct 136 ms 23616 KB answer = YES
87 Correct 142 ms 17656 KB answer = NO
88 Correct 196 ms 18712 KB answer = YES
89 Correct 171 ms 15936 KB answer = YES
90 Correct 159 ms 15924 KB answer = YES
91 Correct 179 ms 15936 KB answer = YES
92 Correct 76 ms 11988 KB answer = YES
93 Correct 79 ms 11772 KB answer = YES
94 Correct 148 ms 23172 KB answer = NO
95 Correct 124 ms 15532 KB answer = NO
96 Correct 315 ms 23872 KB answer = YES
97 Correct 99 ms 23284 KB answer = NO