답안 #1030663

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1030663 2024-07-22T08:16:15 Z ttamx Graph (BOI20_graph) C++17
100 / 100
70 ms 17608 KB
#include<bits/stdc++.h>

using namespace std;

using db = double;

const int N=1e5+5;

int n,m;
vector<pair<int,int>> adj[N];
bool vis[N];
int a[N],b[N],ans[N];
vector<int> comp;
pair<int,int> val;
bool found;

void dfs(int u){
    comp.emplace_back(u);
    vis[u]=true;
    for(auto [v,w]:adj[u]){
        if(!vis[v]){
            a[v]=-a[u];
            b[v]=w-b[u];
            dfs(v);
        }else{
            if(a[u]+a[v]==0){
                if(b[u]+b[v]!=w){
                    cout << "NO",exit(0);
                }
            }else{
                int num=w-b[u]-b[v];
                int den=a[u]+a[v];
                if(!found){
                    found=true;
                    val={num,den};
                }else if(num*val.second!=val.first*den){
                    cout << "NO",exit(0);
                }
            }
        }
    }
}

inline int calc(int x){
    int res=0;
    for(auto u:comp){
        res+=abs(a[u]*x+b[u]*2);
    }
    return res;
}

inline void answer(int x){
    for(auto u:comp){
        ans[u]=a[u]*x+b[u]*2;
    }
}

int main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    cin >> n >> m;
    for(int i=1;i<=m;i++){
        int u,v,w;
        cin >> u >> v >> w;
        adj[u].emplace_back(v,w);
        adj[v].emplace_back(u,w);
    }
    for(int i=1;i<=n;i++)if(!vis[i]){
        comp.clear();
        found=false;
        a[i]=1,b[i]=0;
        dfs(i);
        if(found){
            answer(val.first*2/val.second);
        }else{
            int c=comp.size();
            int l=-c*2,r=c*2;
            while(l<r){
                int m=l+(r-l)/2;
                if(calc(m)<calc(m+1))r=m;
                else l=m+1;
            }
            answer(l);
        }
    }
    cout << "YES\n";
    for(int i=1;i<=n;i++){
        if(ans[i]<0)cout << '-';
        cout << (abs(ans[i])>>1) << '.' << "05"[ans[i]&1] << ' ';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3676 KB answer = YES
2 Correct 1 ms 3676 KB answer = YES
3 Correct 1 ms 3676 KB answer = YES
4 Correct 1 ms 3676 KB answer = NO
5 Correct 1 ms 3676 KB answer = YES
6 Correct 1 ms 3676 KB answer = YES
7 Correct 1 ms 3672 KB answer = YES
8 Correct 1 ms 3712 KB answer = YES
9 Correct 1 ms 3676 KB answer = NO
10 Correct 1 ms 3676 KB answer = YES
11 Correct 1 ms 3676 KB answer = YES
12 Correct 2 ms 3672 KB answer = NO
13 Correct 1 ms 3728 KB answer = YES
14 Correct 1 ms 3676 KB answer = YES
15 Correct 1 ms 3676 KB answer = YES
16 Correct 1 ms 3676 KB answer = YES
17 Correct 1 ms 3676 KB answer = YES
18 Correct 1 ms 3672 KB answer = YES
19 Correct 1 ms 3672 KB answer = YES
20 Correct 1 ms 3676 KB answer = YES
21 Correct 1 ms 3676 KB answer = YES
22 Correct 1 ms 3676 KB answer = NO
23 Correct 1 ms 3676 KB answer = NO
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3676 KB answer = YES
2 Correct 1 ms 3676 KB answer = YES
3 Correct 1 ms 3676 KB answer = YES
4 Correct 1 ms 3676 KB answer = NO
5 Correct 1 ms 3676 KB answer = YES
6 Correct 1 ms 3676 KB answer = YES
7 Correct 1 ms 3672 KB answer = YES
8 Correct 1 ms 3712 KB answer = YES
9 Correct 1 ms 3676 KB answer = NO
10 Correct 1 ms 3676 KB answer = YES
11 Correct 1 ms 3676 KB answer = YES
12 Correct 2 ms 3672 KB answer = NO
13 Correct 1 ms 3728 KB answer = YES
14 Correct 1 ms 3676 KB answer = YES
15 Correct 1 ms 3676 KB answer = YES
16 Correct 1 ms 3676 KB answer = YES
17 Correct 1 ms 3676 KB answer = YES
18 Correct 1 ms 3672 KB answer = YES
19 Correct 1 ms 3672 KB answer = YES
20 Correct 1 ms 3676 KB answer = YES
21 Correct 1 ms 3676 KB answer = YES
22 Correct 1 ms 3676 KB answer = NO
23 Correct 1 ms 3676 KB answer = NO
24 Correct 1 ms 3676 KB answer = YES
25 Correct 1 ms 3676 KB answer = YES
26 Correct 1 ms 3676 KB answer = YES
27 Correct 1 ms 3676 KB answer = YES
28 Correct 1 ms 3676 KB answer = YES
29 Correct 1 ms 3676 KB answer = YES
30 Correct 1 ms 3676 KB answer = NO
31 Correct 1 ms 3672 KB answer = YES
32 Correct 1 ms 3676 KB answer = YES
33 Correct 1 ms 3676 KB answer = YES
34 Correct 1 ms 3672 KB answer = YES
35 Correct 1 ms 3676 KB answer = YES
36 Correct 1 ms 3676 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3676 KB answer = YES
2 Correct 1 ms 3676 KB answer = YES
3 Correct 1 ms 3676 KB answer = YES
4 Correct 1 ms 3676 KB answer = NO
5 Correct 1 ms 3676 KB answer = YES
6 Correct 1 ms 3676 KB answer = YES
7 Correct 1 ms 3672 KB answer = YES
8 Correct 1 ms 3712 KB answer = YES
9 Correct 1 ms 3676 KB answer = NO
10 Correct 1 ms 3676 KB answer = YES
11 Correct 1 ms 3676 KB answer = YES
12 Correct 2 ms 3672 KB answer = NO
13 Correct 1 ms 3728 KB answer = YES
14 Correct 1 ms 3676 KB answer = YES
15 Correct 1 ms 3676 KB answer = YES
16 Correct 1 ms 3676 KB answer = YES
17 Correct 1 ms 3676 KB answer = YES
18 Correct 1 ms 3672 KB answer = YES
19 Correct 1 ms 3672 KB answer = YES
20 Correct 1 ms 3676 KB answer = YES
21 Correct 1 ms 3676 KB answer = YES
22 Correct 1 ms 3676 KB answer = NO
23 Correct 1 ms 3676 KB answer = NO
24 Correct 1 ms 3676 KB answer = YES
25 Correct 1 ms 3676 KB answer = YES
26 Correct 1 ms 3676 KB answer = YES
27 Correct 1 ms 3676 KB answer = YES
28 Correct 1 ms 3676 KB answer = YES
29 Correct 1 ms 3676 KB answer = YES
30 Correct 1 ms 3676 KB answer = NO
31 Correct 1 ms 3672 KB answer = YES
32 Correct 1 ms 3676 KB answer = YES
33 Correct 1 ms 3676 KB answer = YES
34 Correct 1 ms 3672 KB answer = YES
35 Correct 1 ms 3676 KB answer = YES
36 Correct 1 ms 3676 KB answer = YES
37 Correct 1 ms 3676 KB answer = YES
38 Correct 1 ms 3676 KB answer = YES
39 Correct 1 ms 3932 KB answer = YES
40 Correct 1 ms 3928 KB answer = YES
41 Correct 1 ms 3932 KB answer = NO
42 Correct 1 ms 3928 KB answer = YES
43 Correct 1 ms 3932 KB answer = YES
44 Correct 1 ms 3932 KB answer = YES
45 Correct 1 ms 3932 KB answer = YES
46 Correct 1 ms 3676 KB answer = YES
47 Correct 1 ms 3940 KB answer = YES
48 Correct 2 ms 3932 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3676 KB answer = YES
2 Correct 1 ms 3676 KB answer = YES
3 Correct 1 ms 3676 KB answer = YES
4 Correct 1 ms 3676 KB answer = NO
5 Correct 1 ms 3676 KB answer = YES
6 Correct 1 ms 3676 KB answer = YES
7 Correct 1 ms 3672 KB answer = YES
8 Correct 1 ms 3712 KB answer = YES
9 Correct 1 ms 3676 KB answer = NO
10 Correct 1 ms 3676 KB answer = YES
11 Correct 1 ms 3676 KB answer = YES
12 Correct 2 ms 3672 KB answer = NO
13 Correct 1 ms 3728 KB answer = YES
14 Correct 1 ms 3676 KB answer = YES
15 Correct 1 ms 3676 KB answer = YES
16 Correct 1 ms 3676 KB answer = YES
17 Correct 1 ms 3676 KB answer = YES
18 Correct 1 ms 3672 KB answer = YES
19 Correct 1 ms 3672 KB answer = YES
20 Correct 1 ms 3676 KB answer = YES
21 Correct 1 ms 3676 KB answer = YES
22 Correct 1 ms 3676 KB answer = NO
23 Correct 1 ms 3676 KB answer = NO
24 Correct 1 ms 3676 KB answer = YES
25 Correct 1 ms 3676 KB answer = YES
26 Correct 1 ms 3676 KB answer = YES
27 Correct 1 ms 3676 KB answer = YES
28 Correct 1 ms 3676 KB answer = YES
29 Correct 1 ms 3676 KB answer = YES
30 Correct 1 ms 3676 KB answer = NO
31 Correct 1 ms 3672 KB answer = YES
32 Correct 1 ms 3676 KB answer = YES
33 Correct 1 ms 3676 KB answer = YES
34 Correct 1 ms 3672 KB answer = YES
35 Correct 1 ms 3676 KB answer = YES
36 Correct 1 ms 3676 KB answer = YES
37 Correct 1 ms 3676 KB answer = YES
38 Correct 1 ms 3676 KB answer = YES
39 Correct 1 ms 3932 KB answer = YES
40 Correct 1 ms 3928 KB answer = YES
41 Correct 1 ms 3932 KB answer = NO
42 Correct 1 ms 3928 KB answer = YES
43 Correct 1 ms 3932 KB answer = YES
44 Correct 1 ms 3932 KB answer = YES
45 Correct 1 ms 3932 KB answer = YES
46 Correct 1 ms 3676 KB answer = YES
47 Correct 1 ms 3940 KB answer = YES
48 Correct 2 ms 3932 KB answer = YES
49 Correct 5 ms 4444 KB answer = YES
50 Correct 4 ms 4700 KB answer = YES
51 Correct 5 ms 4700 KB answer = YES
52 Correct 3 ms 4144 KB answer = NO
53 Correct 1 ms 3932 KB answer = YES
54 Correct 2 ms 3928 KB answer = YES
55 Correct 3 ms 4188 KB answer = YES
56 Correct 4 ms 4356 KB answer = YES
57 Correct 4 ms 4244 KB answer = YES
58 Correct 4 ms 4188 KB answer = YES
59 Correct 4 ms 4188 KB answer = YES
60 Correct 4 ms 4188 KB answer = YES
61 Correct 2 ms 3932 KB answer = YES
62 Correct 32 ms 8540 KB answer = NO
63 Correct 36 ms 9052 KB answer = YES
64 Correct 34 ms 9080 KB answer = NO
65 Correct 38 ms 9068 KB answer = YES
66 Correct 2 ms 3928 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 3676 KB answer = YES
2 Correct 1 ms 3676 KB answer = YES
3 Correct 1 ms 3676 KB answer = YES
4 Correct 1 ms 3676 KB answer = NO
5 Correct 1 ms 3676 KB answer = YES
6 Correct 1 ms 3676 KB answer = YES
7 Correct 1 ms 3672 KB answer = YES
8 Correct 1 ms 3712 KB answer = YES
9 Correct 1 ms 3676 KB answer = NO
10 Correct 1 ms 3676 KB answer = YES
11 Correct 1 ms 3676 KB answer = YES
12 Correct 2 ms 3672 KB answer = NO
13 Correct 1 ms 3728 KB answer = YES
14 Correct 1 ms 3676 KB answer = YES
15 Correct 1 ms 3676 KB answer = YES
16 Correct 1 ms 3676 KB answer = YES
17 Correct 1 ms 3676 KB answer = YES
18 Correct 1 ms 3672 KB answer = YES
19 Correct 1 ms 3672 KB answer = YES
20 Correct 1 ms 3676 KB answer = YES
21 Correct 1 ms 3676 KB answer = YES
22 Correct 1 ms 3676 KB answer = NO
23 Correct 1 ms 3676 KB answer = NO
24 Correct 1 ms 3676 KB answer = YES
25 Correct 1 ms 3676 KB answer = YES
26 Correct 1 ms 3676 KB answer = YES
27 Correct 1 ms 3676 KB answer = YES
28 Correct 1 ms 3676 KB answer = YES
29 Correct 1 ms 3676 KB answer = YES
30 Correct 1 ms 3676 KB answer = NO
31 Correct 1 ms 3672 KB answer = YES
32 Correct 1 ms 3676 KB answer = YES
33 Correct 1 ms 3676 KB answer = YES
34 Correct 1 ms 3672 KB answer = YES
35 Correct 1 ms 3676 KB answer = YES
36 Correct 1 ms 3676 KB answer = YES
37 Correct 1 ms 3676 KB answer = YES
38 Correct 1 ms 3676 KB answer = YES
39 Correct 1 ms 3932 KB answer = YES
40 Correct 1 ms 3928 KB answer = YES
41 Correct 1 ms 3932 KB answer = NO
42 Correct 1 ms 3928 KB answer = YES
43 Correct 1 ms 3932 KB answer = YES
44 Correct 1 ms 3932 KB answer = YES
45 Correct 1 ms 3932 KB answer = YES
46 Correct 1 ms 3676 KB answer = YES
47 Correct 1 ms 3940 KB answer = YES
48 Correct 2 ms 3932 KB answer = YES
49 Correct 5 ms 4444 KB answer = YES
50 Correct 4 ms 4700 KB answer = YES
51 Correct 5 ms 4700 KB answer = YES
52 Correct 3 ms 4144 KB answer = NO
53 Correct 1 ms 3932 KB answer = YES
54 Correct 2 ms 3928 KB answer = YES
55 Correct 3 ms 4188 KB answer = YES
56 Correct 4 ms 4356 KB answer = YES
57 Correct 4 ms 4244 KB answer = YES
58 Correct 4 ms 4188 KB answer = YES
59 Correct 4 ms 4188 KB answer = YES
60 Correct 4 ms 4188 KB answer = YES
61 Correct 2 ms 3932 KB answer = YES
62 Correct 32 ms 8540 KB answer = NO
63 Correct 36 ms 9052 KB answer = YES
64 Correct 34 ms 9080 KB answer = NO
65 Correct 38 ms 9068 KB answer = YES
66 Correct 2 ms 3928 KB answer = YES
67 Correct 36 ms 14412 KB answer = YES
68 Correct 35 ms 14284 KB answer = YES
69 Correct 33 ms 14288 KB answer = YES
70 Correct 52 ms 17608 KB answer = YES
71 Correct 31 ms 14280 KB answer = YES
72 Correct 41 ms 8652 KB answer = YES
73 Correct 36 ms 8608 KB answer = YES
74 Correct 26 ms 9932 KB answer = YES
75 Correct 15 ms 7772 KB answer = NO
76 Correct 5 ms 4444 KB answer = YES
77 Correct 10 ms 5156 KB answer = YES
78 Correct 17 ms 6112 KB answer = YES
79 Correct 37 ms 8032 KB answer = YES
80 Correct 28 ms 9932 KB answer = YES
81 Correct 25 ms 10616 KB answer = NO
82 Correct 35 ms 13768 KB answer = YES
83 Correct 45 ms 14536 KB answer = YES
84 Correct 48 ms 14280 KB answer = YES
85 Correct 37 ms 14284 KB answer = YES
86 Correct 34 ms 14288 KB answer = YES
87 Correct 36 ms 9044 KB answer = NO
88 Correct 39 ms 10456 KB answer = YES
89 Correct 32 ms 7452 KB answer = YES
90 Correct 31 ms 7504 KB answer = YES
91 Correct 33 ms 7760 KB answer = YES
92 Correct 17 ms 6364 KB answer = YES
93 Correct 17 ms 6368 KB answer = YES
94 Correct 31 ms 13776 KB answer = NO
95 Correct 22 ms 6996 KB answer = NO
96 Correct 70 ms 14764 KB answer = YES
97 Correct 23 ms 13776 KB answer = NO