답안 #1032598

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1032598 2024-07-24T03:37:47 Z 8pete8 Graph (BOI20_graph) C++17
100 / 100
101 ms 36988 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<cassert>
#include<unordered_map>
#include <queue>
#include <cstdint>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-lopps")
#define int long long
using namespace std;
const int mod=998244353,mxn=2e5+5,inf=1e9,minf=-1e9,lg=25;
int n,m,k,x,y;
int type[mxn+10],vis[mxn+10],dist[mxn+10],val[mxn+10];
int coeff[mxn+10];//
double ans[mxn+10];
vector<pii>adj[mxn+10];
stack<pii>st;
int found=0,huh[2]={1,-1};
bool haveans=1;
//chem exam gonna kill me tmr TT
void solve(int cur){
    vector<int>have;
    found=cur;
    while(!st.empty()&&st.top().f!=cur)have.pb(st.top().s),st.pop();
    have.pb(st.top().s),st.pop();
    if(have.size()%2==0)assert(0);
    reverse(all(have));
    ans[cur]=0;
    for(int i=0;i<have.size();i++)ans[cur]+=(huh[i%2]*type[have[i]]);
    ans[cur]=(ans[cur]*1.0/2);
}
void dfs(int cur,int p){
    vis[cur]=1;
    for(auto i:adj[cur]){
        if(i.f==p)continue;
        st.push({cur,i.s});
        if(vis[i.f]){
            //found a cycle, odd cycle -> even dist
            if((found==0)&&((dist[i.f]-dist[cur])%2==0))solve(i.f);
        }
        else dist[i.f]=dist[cur]+1,dfs(i.f,cur);
        if(!st.empty())st.pop();//push and backtrack
    }
}
void getans(int cur){
    for(auto i:adj[cur]){
        if(ans[i.f]!=inf){
            if((ans[cur]+ans[i.f])!=type[i.s])haveans=0;
        }
        else ans[i.f]=(type[i.s]-ans[cur]),getans(i.f);
    }
}
vector<int>comp;
void dfs2(int cur,int p){//all even cycle so we can do bipartite?
    vis[cur]=1;
    comp.pb(cur);
    for(auto i:adj[cur]){
        if(i.f==p)continue;
        if(vis[i.f]&&(coeff[i.f]!=(1^coeff[cur]))&&(val[i.f]!=type[i.s]-val[cur])){
            haveans=0;//shouldnt exist
        }
        if(!vis[i.f]){
            coeff[i.f]=(1^coeff[cur]);
            val[i.f]=type[i.s]-val[cur];
            dfs2(i.f,cur);
        }
    }
}
int32_t main(){
    fastio
    cin>>n>>m;
    for(int i=1;i<=n;i++)ans[i]=inf;
    for(int i=0;i<m;i++){
        int a,b;cin>>a>>b>>type[i];
        adj[a].pb({b,i});
        adj[b].pb({a,i});
    }
    for(int i=1;i<=n;i++)if(!vis[i]){
        found=0;
        dfs(i,-1);
        while(!st.empty())st.pop();
        if(found)getans(found);
    }
    for(int i=1;i<=n;i++)vis[i]=0,val[i]=0,coeff[i]=1;
    vector<int>med;
    for(int i=1;i<=n;i++)if((!vis[i])&&ans[i]==inf){
        comp.clear();
        coeff[i]=1;
        val[i]=0;
        dfs2(i,-1);
        med.clear();
        for(auto j:comp)med.pb(huh[coeff[j]]*val[j]);
        sort(all(med));
        int x=comp.size();
        if((comp.size())%2)ans[i]=med[(x/2)];
        else ans[i]=(med[x/2]+med[(x/2)-1])*1.0/2;
        getans(i);
    }
    //0,-1
    if(!haveans)cout<<"NO\n";
    else{
        cout<<"YES\n";
        for(int i=1;i<=n;i++)if(ans[i]==inf)ans[i]=0;
        for(int i=1;i<=n;i++)cout<<ans[i]<<" ";
    }
    return 0;
    //step 1 find odd cycle
}
/*
observations:
-if there exist an odd cycle the value of the node in the cycle is fixed
because we will have equation like a+b=x,b+c=y.... then we can keep +,- the equation and solve for a
-if we know a value for atleast 1 node we can fill the whole graph

so first we can find if there exist an odd cycle 
-if it exist we can easily solve it
how to find an odd cycle?
dfs keep dist and stack
else?
we will be left with a graph that only have even cycle how to get min sum of abs(x)?
if we can find the value from x then we find other value by
edge cost-val coming from

so x<->a =1
val a=1-x
and b can be like 1-1+x=(x)
or like 2-1+x=(1+x)
we can find each val of node in form of y-x; where x is the starting node
now we want the sum of abs(y-x) for all y value to be min
if(x is negative then we can switch abs(y-x)=abs(x-y))
so 1-x new form will be x-1 and 1+x new form will still be x+1
now with equation like
abs(x-y1),abs(x-y2),abs(x-y3)....
now min sum will just be the min dist from x to all y so finding x is just finding median? of y val
*/

Compilation message

Graph.cpp:32:40: warning: bad option '-funroll-lopps' to pragma 'optimize' [-Wpragmas]
   32 | #pragma GCC optimize ("03,unroll-lopps")
      |                                        ^
Graph.cpp:45:19: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   45 | void solve(int cur){
      |                   ^
Graph.cpp: In function 'void solve(long long int)':
Graph.cpp:53:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |     for(int i=0;i<have.size();i++)ans[cur]+=(huh[i%2]*type[have[i]]);
      |                 ~^~~~~~~~~~~~
Graph.cpp: At global scope:
Graph.cpp:56:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   56 | void dfs(int cur,int p){
      |                       ^
Graph.cpp:69:20: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   69 | void getans(int cur){
      |                    ^
Graph.cpp:78:24: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   78 | void dfs2(int cur,int p){//all even cycle so we can do bipartite?
      |                        ^
Graph.cpp:93:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
   93 | int32_t main(){
      |              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB answer = YES
2 Correct 2 ms 4952 KB answer = YES
3 Correct 2 ms 4960 KB answer = YES
4 Correct 2 ms 5160 KB answer = NO
5 Correct 2 ms 4956 KB answer = YES
6 Correct 2 ms 4964 KB answer = YES
7 Correct 2 ms 4964 KB answer = YES
8 Correct 2 ms 4964 KB answer = YES
9 Correct 2 ms 4956 KB answer = NO
10 Correct 2 ms 4956 KB answer = YES
11 Correct 2 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 2 ms 5204 KB answer = YES
14 Correct 2 ms 4956 KB answer = YES
15 Correct 2 ms 4956 KB answer = YES
16 Correct 2 ms 4956 KB answer = YES
17 Correct 2 ms 4956 KB answer = YES
18 Correct 2 ms 4956 KB answer = YES
19 Correct 3 ms 4952 KB answer = YES
20 Correct 2 ms 4956 KB answer = YES
21 Correct 2 ms 4956 KB answer = YES
22 Correct 3 ms 5004 KB answer = NO
23 Correct 2 ms 4956 KB answer = NO
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB answer = YES
2 Correct 2 ms 4952 KB answer = YES
3 Correct 2 ms 4960 KB answer = YES
4 Correct 2 ms 5160 KB answer = NO
5 Correct 2 ms 4956 KB answer = YES
6 Correct 2 ms 4964 KB answer = YES
7 Correct 2 ms 4964 KB answer = YES
8 Correct 2 ms 4964 KB answer = YES
9 Correct 2 ms 4956 KB answer = NO
10 Correct 2 ms 4956 KB answer = YES
11 Correct 2 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 2 ms 5204 KB answer = YES
14 Correct 2 ms 4956 KB answer = YES
15 Correct 2 ms 4956 KB answer = YES
16 Correct 2 ms 4956 KB answer = YES
17 Correct 2 ms 4956 KB answer = YES
18 Correct 2 ms 4956 KB answer = YES
19 Correct 3 ms 4952 KB answer = YES
20 Correct 2 ms 4956 KB answer = YES
21 Correct 2 ms 4956 KB answer = YES
22 Correct 3 ms 5004 KB answer = NO
23 Correct 2 ms 4956 KB answer = NO
24 Correct 4 ms 5208 KB answer = YES
25 Correct 2 ms 4956 KB answer = YES
26 Correct 2 ms 4952 KB answer = YES
27 Correct 4 ms 4956 KB answer = YES
28 Correct 2 ms 4956 KB answer = YES
29 Correct 3 ms 5164 KB answer = YES
30 Correct 2 ms 4956 KB answer = NO
31 Correct 3 ms 4960 KB answer = YES
32 Correct 3 ms 4956 KB answer = YES
33 Correct 2 ms 4956 KB answer = YES
34 Correct 3 ms 4960 KB answer = YES
35 Correct 2 ms 5160 KB answer = YES
36 Correct 2 ms 4956 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB answer = YES
2 Correct 2 ms 4952 KB answer = YES
3 Correct 2 ms 4960 KB answer = YES
4 Correct 2 ms 5160 KB answer = NO
5 Correct 2 ms 4956 KB answer = YES
6 Correct 2 ms 4964 KB answer = YES
7 Correct 2 ms 4964 KB answer = YES
8 Correct 2 ms 4964 KB answer = YES
9 Correct 2 ms 4956 KB answer = NO
10 Correct 2 ms 4956 KB answer = YES
11 Correct 2 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 2 ms 5204 KB answer = YES
14 Correct 2 ms 4956 KB answer = YES
15 Correct 2 ms 4956 KB answer = YES
16 Correct 2 ms 4956 KB answer = YES
17 Correct 2 ms 4956 KB answer = YES
18 Correct 2 ms 4956 KB answer = YES
19 Correct 3 ms 4952 KB answer = YES
20 Correct 2 ms 4956 KB answer = YES
21 Correct 2 ms 4956 KB answer = YES
22 Correct 3 ms 5004 KB answer = NO
23 Correct 2 ms 4956 KB answer = NO
24 Correct 4 ms 5208 KB answer = YES
25 Correct 2 ms 4956 KB answer = YES
26 Correct 2 ms 4952 KB answer = YES
27 Correct 4 ms 4956 KB answer = YES
28 Correct 2 ms 4956 KB answer = YES
29 Correct 3 ms 5164 KB answer = YES
30 Correct 2 ms 4956 KB answer = NO
31 Correct 3 ms 4960 KB answer = YES
32 Correct 3 ms 4956 KB answer = YES
33 Correct 2 ms 4956 KB answer = YES
34 Correct 3 ms 4960 KB answer = YES
35 Correct 2 ms 5160 KB answer = YES
36 Correct 2 ms 4956 KB answer = YES
37 Correct 3 ms 5216 KB answer = YES
38 Correct 2 ms 5216 KB answer = YES
39 Correct 3 ms 5212 KB answer = YES
40 Correct 3 ms 5172 KB answer = YES
41 Correct 2 ms 5212 KB answer = NO
42 Correct 3 ms 5172 KB answer = YES
43 Correct 3 ms 5220 KB answer = YES
44 Correct 3 ms 5212 KB answer = YES
45 Correct 3 ms 5208 KB answer = YES
46 Correct 4 ms 5216 KB answer = YES
47 Correct 4 ms 5220 KB answer = YES
48 Correct 3 ms 5212 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB answer = YES
2 Correct 2 ms 4952 KB answer = YES
3 Correct 2 ms 4960 KB answer = YES
4 Correct 2 ms 5160 KB answer = NO
5 Correct 2 ms 4956 KB answer = YES
6 Correct 2 ms 4964 KB answer = YES
7 Correct 2 ms 4964 KB answer = YES
8 Correct 2 ms 4964 KB answer = YES
9 Correct 2 ms 4956 KB answer = NO
10 Correct 2 ms 4956 KB answer = YES
11 Correct 2 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 2 ms 5204 KB answer = YES
14 Correct 2 ms 4956 KB answer = YES
15 Correct 2 ms 4956 KB answer = YES
16 Correct 2 ms 4956 KB answer = YES
17 Correct 2 ms 4956 KB answer = YES
18 Correct 2 ms 4956 KB answer = YES
19 Correct 3 ms 4952 KB answer = YES
20 Correct 2 ms 4956 KB answer = YES
21 Correct 2 ms 4956 KB answer = YES
22 Correct 3 ms 5004 KB answer = NO
23 Correct 2 ms 4956 KB answer = NO
24 Correct 4 ms 5208 KB answer = YES
25 Correct 2 ms 4956 KB answer = YES
26 Correct 2 ms 4952 KB answer = YES
27 Correct 4 ms 4956 KB answer = YES
28 Correct 2 ms 4956 KB answer = YES
29 Correct 3 ms 5164 KB answer = YES
30 Correct 2 ms 4956 KB answer = NO
31 Correct 3 ms 4960 KB answer = YES
32 Correct 3 ms 4956 KB answer = YES
33 Correct 2 ms 4956 KB answer = YES
34 Correct 3 ms 4960 KB answer = YES
35 Correct 2 ms 5160 KB answer = YES
36 Correct 2 ms 4956 KB answer = YES
37 Correct 3 ms 5216 KB answer = YES
38 Correct 2 ms 5216 KB answer = YES
39 Correct 3 ms 5212 KB answer = YES
40 Correct 3 ms 5172 KB answer = YES
41 Correct 2 ms 5212 KB answer = NO
42 Correct 3 ms 5172 KB answer = YES
43 Correct 3 ms 5220 KB answer = YES
44 Correct 3 ms 5212 KB answer = YES
45 Correct 3 ms 5208 KB answer = YES
46 Correct 4 ms 5216 KB answer = YES
47 Correct 4 ms 5220 KB answer = YES
48 Correct 3 ms 5212 KB answer = YES
49 Correct 8 ms 6492 KB answer = YES
50 Correct 8 ms 6984 KB answer = YES
51 Correct 8 ms 7008 KB answer = YES
52 Correct 5 ms 7004 KB answer = NO
53 Correct 3 ms 5092 KB answer = YES
54 Correct 4 ms 5356 KB answer = YES
55 Correct 5 ms 5688 KB answer = YES
56 Correct 8 ms 6492 KB answer = YES
57 Correct 8 ms 6236 KB answer = YES
58 Correct 7 ms 6236 KB answer = YES
59 Correct 7 ms 5980 KB answer = YES
60 Correct 12 ms 6492 KB answer = YES
61 Correct 5 ms 5720 KB answer = YES
62 Correct 43 ms 19188 KB answer = NO
63 Correct 46 ms 19280 KB answer = YES
64 Correct 43 ms 19284 KB answer = NO
65 Correct 44 ms 19276 KB answer = YES
66 Correct 3 ms 5212 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB answer = YES
2 Correct 2 ms 4952 KB answer = YES
3 Correct 2 ms 4960 KB answer = YES
4 Correct 2 ms 5160 KB answer = NO
5 Correct 2 ms 4956 KB answer = YES
6 Correct 2 ms 4964 KB answer = YES
7 Correct 2 ms 4964 KB answer = YES
8 Correct 2 ms 4964 KB answer = YES
9 Correct 2 ms 4956 KB answer = NO
10 Correct 2 ms 4956 KB answer = YES
11 Correct 2 ms 4956 KB answer = YES
12 Correct 2 ms 4956 KB answer = NO
13 Correct 2 ms 5204 KB answer = YES
14 Correct 2 ms 4956 KB answer = YES
15 Correct 2 ms 4956 KB answer = YES
16 Correct 2 ms 4956 KB answer = YES
17 Correct 2 ms 4956 KB answer = YES
18 Correct 2 ms 4956 KB answer = YES
19 Correct 3 ms 4952 KB answer = YES
20 Correct 2 ms 4956 KB answer = YES
21 Correct 2 ms 4956 KB answer = YES
22 Correct 3 ms 5004 KB answer = NO
23 Correct 2 ms 4956 KB answer = NO
24 Correct 4 ms 5208 KB answer = YES
25 Correct 2 ms 4956 KB answer = YES
26 Correct 2 ms 4952 KB answer = YES
27 Correct 4 ms 4956 KB answer = YES
28 Correct 2 ms 4956 KB answer = YES
29 Correct 3 ms 5164 KB answer = YES
30 Correct 2 ms 4956 KB answer = NO
31 Correct 3 ms 4960 KB answer = YES
32 Correct 3 ms 4956 KB answer = YES
33 Correct 2 ms 4956 KB answer = YES
34 Correct 3 ms 4960 KB answer = YES
35 Correct 2 ms 5160 KB answer = YES
36 Correct 2 ms 4956 KB answer = YES
37 Correct 3 ms 5216 KB answer = YES
38 Correct 2 ms 5216 KB answer = YES
39 Correct 3 ms 5212 KB answer = YES
40 Correct 3 ms 5172 KB answer = YES
41 Correct 2 ms 5212 KB answer = NO
42 Correct 3 ms 5172 KB answer = YES
43 Correct 3 ms 5220 KB answer = YES
44 Correct 3 ms 5212 KB answer = YES
45 Correct 3 ms 5208 KB answer = YES
46 Correct 4 ms 5216 KB answer = YES
47 Correct 4 ms 5220 KB answer = YES
48 Correct 3 ms 5212 KB answer = YES
49 Correct 8 ms 6492 KB answer = YES
50 Correct 8 ms 6984 KB answer = YES
51 Correct 8 ms 7008 KB answer = YES
52 Correct 5 ms 7004 KB answer = NO
53 Correct 3 ms 5092 KB answer = YES
54 Correct 4 ms 5356 KB answer = YES
55 Correct 5 ms 5688 KB answer = YES
56 Correct 8 ms 6492 KB answer = YES
57 Correct 8 ms 6236 KB answer = YES
58 Correct 7 ms 6236 KB answer = YES
59 Correct 7 ms 5980 KB answer = YES
60 Correct 12 ms 6492 KB answer = YES
61 Correct 5 ms 5720 KB answer = YES
62 Correct 43 ms 19188 KB answer = NO
63 Correct 46 ms 19280 KB answer = YES
64 Correct 43 ms 19284 KB answer = NO
65 Correct 44 ms 19276 KB answer = YES
66 Correct 3 ms 5212 KB answer = YES
67 Correct 62 ms 30344 KB answer = YES
68 Correct 61 ms 30292 KB answer = YES
69 Correct 61 ms 28988 KB answer = YES
70 Correct 78 ms 36988 KB answer = YES
71 Correct 60 ms 28984 KB answer = YES
72 Correct 62 ms 18636 KB answer = YES
73 Correct 56 ms 16392 KB answer = YES
74 Correct 40 ms 18672 KB answer = YES
75 Correct 29 ms 19444 KB answer = NO
76 Correct 8 ms 6744 KB answer = YES
77 Correct 22 ms 8408 KB answer = YES
78 Correct 34 ms 10916 KB answer = YES
79 Correct 54 ms 16856 KB answer = YES
80 Correct 44 ms 19548 KB answer = YES
81 Correct 40 ms 22244 KB answer = NO
82 Correct 81 ms 27300 KB answer = YES
83 Correct 94 ms 29500 KB answer = YES
84 Correct 82 ms 30800 KB answer = YES
85 Correct 59 ms 30296 KB answer = YES
86 Correct 54 ms 28760 KB answer = YES
87 Correct 56 ms 20884 KB answer = NO
88 Correct 76 ms 22536 KB answer = YES
89 Correct 54 ms 16060 KB answer = YES
90 Correct 61 ms 16020 KB answer = YES
91 Correct 63 ms 16720 KB answer = YES
92 Correct 29 ms 10840 KB answer = YES
93 Correct 30 ms 10840 KB answer = YES
94 Correct 58 ms 30544 KB answer = NO
95 Correct 38 ms 16316 KB answer = NO
96 Correct 101 ms 31568 KB answer = YES
97 Correct 35 ms 30032 KB answer = NO