Submission #781150

# Submission time Handle Problem Language Result Execution time Memory
781150 2023-07-12T19:32:20 Z FEDIKUS Graph (BOI20_graph) C++17
100 / 100
192 ms 27732 KB
#include<bits/stdc++.h>

using namespace std;

using ll = long long;

const ll maxn=1e5+10;

vector<pair<ll,ll> > g[maxn];
bool moze=true;
bool vis[maxn];
ll val[maxn];
ll sgn[maxn];
double res[maxn];
vector<int> comp;
set<ll> a;

void dfs(ll node){
    comp.push_back(node);
    vis[node]=true;
    for(auto i:g[node]){
        if(vis[i.first]){
            if(sgn[i.first]!=sgn[node] && val[i.first]+val[node]!=i.second) moze=false;
            if(sgn[i.first]==sgn[node]){
                if(sgn[i.first]==1) a.emplace(i.second-val[node]-val[i.first]);
                else a.emplace(val[i.first]+val[node]-i.second);
            }
        }else{
            sgn[i.first]=-sgn[node];
            val[i.first]=i.second-val[node];
            dfs(i.first);
        }
    }
}

int main(){
    ll n,m;
    cin>>n>>m;
    for(ll i=0;i<m;i++){
        ll u,v,c;
        cin>>u>>v>>c;
        g[u].push_back({v,c});
        g[v].push_back({u,c});
    }
    for(int i=1;i<=n;i++){

        if(vis[i]) continue;
        sgn[i]=1;
        val[i]=0;
        comp.clear();
        a.clear();
        dfs(i);

        if(a.size()>1) moze=false;
        if(a.size()==1){
            double aa=*a.begin();
            for(ll i:comp){
                res[i]=aa*sgn[i]/2+val[i];
            }
            continue;
        }

        pair<ll,int> best={LLONG_MAX,0};

        vector<int> kandidati;
        for(ll i:comp) kandidati.push_back(-val[i]*sgn[i]);
        sort(kandidati.begin(),kandidati.end());

        vector<int> poz;
        vector<int> neg;
        for(int i:comp){
            if(sgn[i]>0) poz.push_back(val[i]);
            else neg.push_back(val[i]);
        }
        sort(poz.begin(),poz.end(),greater<int>());
        sort(neg.begin(),neg.end());

        ll pozl=0;
        ll sumpoz=0;
        ll negl=0;
        ll sumneg=0;
        for(int i:poz) sumpoz+=i;
        for(int i:neg) sumneg+=i;

        int ipoz=0;
        int ineg=0;

        for(int i:kandidati){
            while(ipoz<poz.size() && poz[ipoz]+i>=0) {pozl+=poz[ipoz]; ipoz++;}
            while(ineg<neg.size() && neg[ineg]-i<=0) {negl+=neg[ineg]; ineg++;}
            
            ll sum=0;

            sum+=pozl+ipoz*i;
            sum-=(sumpoz-pozl)+(poz.size()-ipoz)*i;

            sum-=negl-ineg*i;
            sum+=(sumneg-negl)-(neg.size()-ineg)*i;

            best=min(best,{sum,i});
        }
        
        for(ll i:comp){
            res[i]=val[i]+best.second*sgn[i];
        }
    }
    if(moze){
        cout<<"YES\n";
        cout<<fixed<<showpoint<<setprecision(15);
        for(int i=1;i<=n;i++){
            cout<<res[i];
            cout<<" ";
        }
    }else cout<<"NO\n";
    return 0;
}

Compilation message

Graph.cpp: In function 'int main()':
Graph.cpp:89:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |             while(ipoz<poz.size() && poz[ipoz]+i>=0) {pozl+=poz[ipoz]; ipoz++;}
      |                   ~~~~^~~~~~~~~~~
Graph.cpp:90:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |             while(ineg<neg.size() && neg[ineg]-i<=0) {negl+=neg[ineg]; ineg++;}
      |                   ~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 2 ms 2644 KB answer = YES
4 Correct 1 ms 2644 KB answer = NO
5 Correct 1 ms 2644 KB answer = YES
6 Correct 1 ms 2644 KB answer = YES
7 Correct 6 ms 2660 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 1 ms 2644 KB answer = NO
10 Correct 1 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 1 ms 2644 KB answer = NO
13 Correct 1 ms 2644 KB answer = YES
14 Correct 1 ms 2644 KB answer = YES
15 Correct 1 ms 2644 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 2644 KB answer = YES
22 Correct 1 ms 2644 KB answer = NO
23 Correct 1 ms 2644 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 2 ms 2644 KB answer = YES
4 Correct 1 ms 2644 KB answer = NO
5 Correct 1 ms 2644 KB answer = YES
6 Correct 1 ms 2644 KB answer = YES
7 Correct 6 ms 2660 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 1 ms 2644 KB answer = NO
10 Correct 1 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 1 ms 2644 KB answer = NO
13 Correct 1 ms 2644 KB answer = YES
14 Correct 1 ms 2644 KB answer = YES
15 Correct 1 ms 2644 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 2644 KB answer = YES
22 Correct 1 ms 2644 KB answer = NO
23 Correct 1 ms 2644 KB answer = NO
24 Correct 1 ms 2644 KB answer = YES
25 Correct 1 ms 2644 KB answer = YES
26 Correct 1 ms 2644 KB answer = YES
27 Correct 2 ms 2644 KB answer = YES
28 Correct 1 ms 2644 KB answer = YES
29 Correct 2 ms 2688 KB answer = YES
30 Correct 1 ms 2644 KB answer = NO
31 Correct 2 ms 2644 KB answer = YES
32 Correct 1 ms 2644 KB answer = YES
33 Correct 1 ms 2644 KB answer = YES
34 Correct 1 ms 2644 KB answer = YES
35 Correct 1 ms 2588 KB answer = YES
36 Correct 1 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 2 ms 2644 KB answer = YES
4 Correct 1 ms 2644 KB answer = NO
5 Correct 1 ms 2644 KB answer = YES
6 Correct 1 ms 2644 KB answer = YES
7 Correct 6 ms 2660 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 1 ms 2644 KB answer = NO
10 Correct 1 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 1 ms 2644 KB answer = NO
13 Correct 1 ms 2644 KB answer = YES
14 Correct 1 ms 2644 KB answer = YES
15 Correct 1 ms 2644 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 2644 KB answer = YES
22 Correct 1 ms 2644 KB answer = NO
23 Correct 1 ms 2644 KB answer = NO
24 Correct 1 ms 2644 KB answer = YES
25 Correct 1 ms 2644 KB answer = YES
26 Correct 1 ms 2644 KB answer = YES
27 Correct 2 ms 2644 KB answer = YES
28 Correct 1 ms 2644 KB answer = YES
29 Correct 2 ms 2688 KB answer = YES
30 Correct 1 ms 2644 KB answer = NO
31 Correct 2 ms 2644 KB answer = YES
32 Correct 1 ms 2644 KB answer = YES
33 Correct 1 ms 2644 KB answer = YES
34 Correct 1 ms 2644 KB answer = YES
35 Correct 1 ms 2588 KB answer = YES
36 Correct 1 ms 2644 KB answer = YES
37 Correct 1 ms 2644 KB answer = YES
38 Correct 1 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 3 ms 2716 KB answer = YES
43 Correct 3 ms 2768 KB answer = YES
44 Correct 2 ms 2772 KB answer = YES
45 Correct 2 ms 2772 KB answer = YES
46 Correct 2 ms 2644 KB answer = YES
47 Correct 2 ms 2772 KB answer = YES
48 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 2 ms 2644 KB answer = YES
4 Correct 1 ms 2644 KB answer = NO
5 Correct 1 ms 2644 KB answer = YES
6 Correct 1 ms 2644 KB answer = YES
7 Correct 6 ms 2660 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 1 ms 2644 KB answer = NO
10 Correct 1 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 1 ms 2644 KB answer = NO
13 Correct 1 ms 2644 KB answer = YES
14 Correct 1 ms 2644 KB answer = YES
15 Correct 1 ms 2644 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 2644 KB answer = YES
22 Correct 1 ms 2644 KB answer = NO
23 Correct 1 ms 2644 KB answer = NO
24 Correct 1 ms 2644 KB answer = YES
25 Correct 1 ms 2644 KB answer = YES
26 Correct 1 ms 2644 KB answer = YES
27 Correct 2 ms 2644 KB answer = YES
28 Correct 1 ms 2644 KB answer = YES
29 Correct 2 ms 2688 KB answer = YES
30 Correct 1 ms 2644 KB answer = NO
31 Correct 2 ms 2644 KB answer = YES
32 Correct 1 ms 2644 KB answer = YES
33 Correct 1 ms 2644 KB answer = YES
34 Correct 1 ms 2644 KB answer = YES
35 Correct 1 ms 2588 KB answer = YES
36 Correct 1 ms 2644 KB answer = YES
37 Correct 1 ms 2644 KB answer = YES
38 Correct 1 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 3 ms 2716 KB answer = YES
43 Correct 3 ms 2768 KB answer = YES
44 Correct 2 ms 2772 KB answer = YES
45 Correct 2 ms 2772 KB answer = YES
46 Correct 2 ms 2644 KB answer = YES
47 Correct 2 ms 2772 KB answer = YES
48 Correct 3 ms 2772 KB answer = YES
49 Correct 13 ms 3772 KB answer = YES
50 Correct 12 ms 4140 KB answer = YES
51 Correct 18 ms 4248 KB answer = YES
52 Correct 8 ms 4052 KB answer = NO
53 Correct 3 ms 2772 KB answer = YES
54 Correct 4 ms 2900 KB answer = YES
55 Correct 7 ms 3192 KB answer = YES
56 Correct 11 ms 3760 KB answer = YES
57 Correct 11 ms 3592 KB answer = YES
58 Correct 11 ms 3540 KB answer = YES
59 Correct 10 ms 3560 KB answer = YES
60 Correct 12 ms 3668 KB answer = YES
61 Correct 10 ms 3188 KB answer = YES
62 Correct 115 ms 12688 KB answer = NO
63 Correct 108 ms 12844 KB answer = YES
64 Correct 108 ms 12756 KB answer = NO
65 Correct 108 ms 12876 KB answer = YES
66 Correct 4 ms 2852 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 2 ms 2644 KB answer = YES
4 Correct 1 ms 2644 KB answer = NO
5 Correct 1 ms 2644 KB answer = YES
6 Correct 1 ms 2644 KB answer = YES
7 Correct 6 ms 2660 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 1 ms 2644 KB answer = NO
10 Correct 1 ms 2644 KB answer = YES
11 Correct 2 ms 2644 KB answer = YES
12 Correct 1 ms 2644 KB answer = NO
13 Correct 1 ms 2644 KB answer = YES
14 Correct 1 ms 2644 KB answer = YES
15 Correct 1 ms 2644 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 2644 KB answer = YES
22 Correct 1 ms 2644 KB answer = NO
23 Correct 1 ms 2644 KB answer = NO
24 Correct 1 ms 2644 KB answer = YES
25 Correct 1 ms 2644 KB answer = YES
26 Correct 1 ms 2644 KB answer = YES
27 Correct 2 ms 2644 KB answer = YES
28 Correct 1 ms 2644 KB answer = YES
29 Correct 2 ms 2688 KB answer = YES
30 Correct 1 ms 2644 KB answer = NO
31 Correct 2 ms 2644 KB answer = YES
32 Correct 1 ms 2644 KB answer = YES
33 Correct 1 ms 2644 KB answer = YES
34 Correct 1 ms 2644 KB answer = YES
35 Correct 1 ms 2588 KB answer = YES
36 Correct 1 ms 2644 KB answer = YES
37 Correct 1 ms 2644 KB answer = YES
38 Correct 1 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 3 ms 2716 KB answer = YES
43 Correct 3 ms 2768 KB answer = YES
44 Correct 2 ms 2772 KB answer = YES
45 Correct 2 ms 2772 KB answer = YES
46 Correct 2 ms 2644 KB answer = YES
47 Correct 2 ms 2772 KB answer = YES
48 Correct 3 ms 2772 KB answer = YES
49 Correct 13 ms 3772 KB answer = YES
50 Correct 12 ms 4140 KB answer = YES
51 Correct 18 ms 4248 KB answer = YES
52 Correct 8 ms 4052 KB answer = NO
53 Correct 3 ms 2772 KB answer = YES
54 Correct 4 ms 2900 KB answer = YES
55 Correct 7 ms 3192 KB answer = YES
56 Correct 11 ms 3760 KB answer = YES
57 Correct 11 ms 3592 KB answer = YES
58 Correct 11 ms 3540 KB answer = YES
59 Correct 10 ms 3560 KB answer = YES
60 Correct 12 ms 3668 KB answer = YES
61 Correct 10 ms 3188 KB answer = YES
62 Correct 115 ms 12688 KB answer = NO
63 Correct 108 ms 12844 KB answer = YES
64 Correct 108 ms 12756 KB answer = NO
65 Correct 108 ms 12876 KB answer = YES
66 Correct 4 ms 2852 KB answer = YES
67 Correct 122 ms 21588 KB answer = YES
68 Correct 107 ms 21388 KB answer = YES
69 Correct 118 ms 21480 KB answer = YES
70 Correct 173 ms 27732 KB answer = YES
71 Correct 107 ms 21448 KB answer = YES
72 Correct 119 ms 12616 KB answer = YES
73 Correct 106 ms 12544 KB answer = YES
74 Correct 97 ms 13760 KB answer = YES
75 Correct 48 ms 13056 KB answer = NO
76 Correct 14 ms 3964 KB answer = YES
77 Correct 27 ms 5228 KB answer = YES
78 Correct 47 ms 6980 KB answer = YES
79 Correct 104 ms 11096 KB answer = YES
80 Correct 80 ms 13716 KB answer = YES
81 Correct 69 ms 15120 KB answer = NO
82 Correct 124 ms 20704 KB answer = YES
83 Correct 130 ms 22168 KB answer = YES
84 Correct 133 ms 22080 KB answer = YES
85 Correct 148 ms 21512 KB answer = YES
86 Correct 113 ms 21548 KB answer = YES
87 Correct 97 ms 13744 KB answer = NO
88 Correct 139 ms 16444 KB answer = YES
89 Correct 115 ms 11576 KB answer = YES
90 Correct 107 ms 11592 KB answer = YES
91 Correct 124 ms 12068 KB answer = YES
92 Correct 53 ms 7620 KB answer = YES
93 Correct 52 ms 7612 KB answer = YES
94 Correct 84 ms 21112 KB answer = NO
95 Correct 83 ms 10288 KB answer = NO
96 Correct 192 ms 23380 KB answer = YES
97 Correct 69 ms 20612 KB answer = NO