Submission #522003

# Submission time Handle Problem Language Result Execution time Memory
522003 2022-02-03T15:12:29 Z ignaciocanta Graph (BOI20_graph) C++14
100 / 100
159 ms 22428 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using Tree = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
 
using tint = long long;
using ld = long double;
 
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)	
 
using pi = pair<int,int>;
using pl = pair<tint,tint>;
using vi = vector<int>;
using vs = vector<string>;
using vl = vector<tint>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
 
#define pb push_back
#define pf push_front
#define rsz resize
#define all(x) begin(x), end(x)
#define rall(x) x.rbegin(), x.rend() 
#define sz(x) (int)(x).size()
#define ins insert
 
#define f first
#define s second
#define mp make_pair
 
#define DBG(x) cerr << #x << " = " << x << endl;
 
const int MOD = 1e9+7; 
const int mod = 998244353;
const int MX = 1e5+5;
const tint INF = 1e18; 
const int inf = 1e9;
const ld PI = acos(ld(-1)); 
const double eps = 1e-9;
 
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
 
template<class T> void remDup(vector<T> &v){ 
	sort(all(v)); v.erase(unique(all(v)),end(v));
}
 
template<class T> bool ckmin(T& a, const T& b) {
	return b < a ? a = b, 1 : 0; 
} 
template<class T> bool ckmax(T& a, const T& b) {
	return a < b ? a = b, 1 : 0; 
}
 
bool valid(int x, int y, int n, int m){
	return (0<=x && x<n && 0<=y && y<m);
}
 
int cdiv(int a, int b) { return a/b+((a^b)>0&&a%b); } //redondea p arriba
int fdiv(int a, int b) { return a/b-((a^b)<0&&a%b); } //redondea p abajo
 
void NACHO(string name = ""){
	ios_base::sync_with_stdio(0); cin.tie(0);
	if(sz(name)){
		freopen((name+".in").c_str(), "r", stdin);
		freopen((name+".out").c_str(), "w", stdout);
	}
}

vpi adj[MX];
pi eq[MX];
double val[MX];
bool solvedComp[MX];
bool possible = 1;
double x = inf;

double solveEq(pi a, pi b){
	// ax+b = cx+d
	// (a-c)x = d-b
	// x = (d-b)/(a-c)
	if(a.f - b.f == 0){
		return 0;
	}
	return double(double(b.s - a.s) / double(a.f - b.f));
}

vi curComp;

void dfs(int node, int pa){
	solvedComp[node] = 1;
	curComp.pb(node);
	trav(u, adj[node]){
		if(u.f == pa) continue;
		if(!solvedComp[u.f]){
			if(u.s == 1){
				eq[u.f] = mp(-eq[node].f, -eq[node].s + 1);
			}else{
				eq[u.f] = mp(-eq[node].f, -eq[node].s + 2);
			}
			dfs(u.f, node);
		}else{
			// linear equation has already been assigned.
			// we can now find val[neighbor].
			bool bothEqual = 0;
			double ret = inf;
			if(u.s == 1){
				pi cur =  mp(-eq[node].f, -eq[node].s + 1);
				if(eq[u.f].f == cur.f && eq[u.f].s == cur.s) bothEqual = 1;
				if(eq[u.f].f == cur.f && eq[u.f].s != cur.s) possible = 0;
				ret = solveEq(cur, eq[u.f]);
			}else{
				pi cur =  mp(-eq[node].f, -eq[node].s + 2);
				if(eq[u.f].f == cur.f && eq[u.f].s == cur.s) bothEqual = 1;
				if(eq[u.f].f == cur.f && eq[u.f].s != cur.s) possible = 0;
				ret = solveEq(cur, eq[u.f]);
			}
			if(!bothEqual) x = ret;
		}
	}
}

void solveComp(int node){
	curComp.clear();
	x = inf;
	eq[node] = mp(1, 0);
	dfs(node, -1);
	if(x != inf){
		// at least one node had more than one equation,
		// so we could calculate x.
		trav(u, curComp) val[u] = eq[u].f * x + eq[u].s;
	}else{
		// we must find x s.t
		// |ax+b|+|cx+d|+|ex+f|+... is minimized
		// as slopes can only be 1 or -1
		// this problem is esentially
		// find x which minimizes |a1-x|+|a2-x|+...
		// it is well known that x equals the median of a1, a2, ..., an
		vi slopes;
		trav(u, curComp) slopes.pb(-eq[u].f * eq[u].s);
		sort(all(slopes));
		if(sz(slopes) & 1) x = slopes[sz(slopes)/2];
		else x = (slopes[sz(slopes)/2] + slopes[sz(slopes)/2-1]) / 2;
		trav(u, curComp) val[u] = eq[u].f * x + eq[u].s;
	}
}

int main(){
	NACHO();
	int n, m; cin >> n >> m;
	F0R(i, m){
		int u, v, c; cin >> u >> v >> c; 
		--u, --v;
		adj[u].pb(mp(v, c));
		adj[v].pb(mp(u, c));
	}
	F0R(i, n) val[i] = 3;
	F0R(i, n){
		if(!solvedComp[i]){
			solveComp(i);
		}
	}
	F0R(i, n){
		trav(u, adj[i]){
			if(fabs(val[i] + val[u.f] - u.s) >= eps) possible = 0;
		}
	}
	if(!possible) cout << "NO\n";
	else{
		cout << "YES\n";
		F0R(i, n){
			cout << fixed << setprecision(6) << val[i] << " ";
		}
		cout << "\n";
	}
}

Compilation message

Graph.cpp: In function 'void NACHO(std::string)':
Graph.cpp:76:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |   freopen((name+".in").c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Graph.cpp:77:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |   freopen((name+".out").c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2764 KB answer = YES
2 Correct 2 ms 2668 KB answer = YES
3 Correct 2 ms 2636 KB answer = YES
4 Correct 2 ms 2636 KB answer = NO
5 Correct 2 ms 2672 KB answer = YES
6 Correct 2 ms 2636 KB answer = YES
7 Correct 2 ms 2636 KB answer = YES
8 Correct 2 ms 2668 KB answer = YES
9 Correct 2 ms 2636 KB answer = NO
10 Correct 2 ms 2660 KB answer = YES
11 Correct 2 ms 2636 KB answer = YES
12 Correct 2 ms 2636 KB answer = NO
13 Correct 2 ms 2668 KB answer = YES
14 Correct 2 ms 2636 KB answer = YES
15 Correct 2 ms 2764 KB answer = YES
16 Correct 2 ms 2664 KB answer = YES
17 Correct 2 ms 2636 KB answer = YES
18 Correct 2 ms 2636 KB answer = YES
19 Correct 1 ms 2664 KB answer = YES
20 Correct 2 ms 2636 KB answer = YES
21 Correct 2 ms 2636 KB answer = YES
22 Correct 2 ms 2636 KB answer = NO
23 Correct 2 ms 2636 KB answer = NO
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2764 KB answer = YES
2 Correct 2 ms 2668 KB answer = YES
3 Correct 2 ms 2636 KB answer = YES
4 Correct 2 ms 2636 KB answer = NO
5 Correct 2 ms 2672 KB answer = YES
6 Correct 2 ms 2636 KB answer = YES
7 Correct 2 ms 2636 KB answer = YES
8 Correct 2 ms 2668 KB answer = YES
9 Correct 2 ms 2636 KB answer = NO
10 Correct 2 ms 2660 KB answer = YES
11 Correct 2 ms 2636 KB answer = YES
12 Correct 2 ms 2636 KB answer = NO
13 Correct 2 ms 2668 KB answer = YES
14 Correct 2 ms 2636 KB answer = YES
15 Correct 2 ms 2764 KB answer = YES
16 Correct 2 ms 2664 KB answer = YES
17 Correct 2 ms 2636 KB answer = YES
18 Correct 2 ms 2636 KB answer = YES
19 Correct 1 ms 2664 KB answer = YES
20 Correct 2 ms 2636 KB answer = YES
21 Correct 2 ms 2636 KB answer = YES
22 Correct 2 ms 2636 KB answer = NO
23 Correct 2 ms 2636 KB answer = NO
24 Correct 2 ms 2652 KB answer = YES
25 Correct 2 ms 2636 KB answer = YES
26 Correct 2 ms 2636 KB answer = YES
27 Correct 2 ms 2636 KB answer = YES
28 Correct 2 ms 2636 KB answer = YES
29 Correct 2 ms 2636 KB answer = YES
30 Correct 2 ms 2636 KB answer = NO
31 Correct 2 ms 2668 KB answer = YES
32 Correct 2 ms 2636 KB answer = YES
33 Correct 2 ms 2648 KB answer = YES
34 Correct 2 ms 2636 KB answer = YES
35 Correct 2 ms 2672 KB answer = YES
36 Correct 2 ms 2636 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2764 KB answer = YES
2 Correct 2 ms 2668 KB answer = YES
3 Correct 2 ms 2636 KB answer = YES
4 Correct 2 ms 2636 KB answer = NO
5 Correct 2 ms 2672 KB answer = YES
6 Correct 2 ms 2636 KB answer = YES
7 Correct 2 ms 2636 KB answer = YES
8 Correct 2 ms 2668 KB answer = YES
9 Correct 2 ms 2636 KB answer = NO
10 Correct 2 ms 2660 KB answer = YES
11 Correct 2 ms 2636 KB answer = YES
12 Correct 2 ms 2636 KB answer = NO
13 Correct 2 ms 2668 KB answer = YES
14 Correct 2 ms 2636 KB answer = YES
15 Correct 2 ms 2764 KB answer = YES
16 Correct 2 ms 2664 KB answer = YES
17 Correct 2 ms 2636 KB answer = YES
18 Correct 2 ms 2636 KB answer = YES
19 Correct 1 ms 2664 KB answer = YES
20 Correct 2 ms 2636 KB answer = YES
21 Correct 2 ms 2636 KB answer = YES
22 Correct 2 ms 2636 KB answer = NO
23 Correct 2 ms 2636 KB answer = NO
24 Correct 2 ms 2652 KB answer = YES
25 Correct 2 ms 2636 KB answer = YES
26 Correct 2 ms 2636 KB answer = YES
27 Correct 2 ms 2636 KB answer = YES
28 Correct 2 ms 2636 KB answer = YES
29 Correct 2 ms 2636 KB answer = YES
30 Correct 2 ms 2636 KB answer = NO
31 Correct 2 ms 2668 KB answer = YES
32 Correct 2 ms 2636 KB answer = YES
33 Correct 2 ms 2648 KB answer = YES
34 Correct 2 ms 2636 KB answer = YES
35 Correct 2 ms 2672 KB answer = YES
36 Correct 2 ms 2636 KB answer = YES
37 Correct 2 ms 2636 KB answer = YES
38 Correct 1 ms 2636 KB answer = YES
39 Correct 2 ms 2636 KB answer = YES
40 Correct 2 ms 2764 KB answer = YES
41 Correct 2 ms 2636 KB answer = NO
42 Correct 2 ms 2764 KB answer = YES
43 Correct 2 ms 2764 KB answer = YES
44 Correct 3 ms 2636 KB answer = YES
45 Correct 3 ms 2676 KB answer = YES
46 Correct 2 ms 2648 KB answer = YES
47 Correct 2 ms 2672 KB answer = YES
48 Correct 2 ms 2764 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2764 KB answer = YES
2 Correct 2 ms 2668 KB answer = YES
3 Correct 2 ms 2636 KB answer = YES
4 Correct 2 ms 2636 KB answer = NO
5 Correct 2 ms 2672 KB answer = YES
6 Correct 2 ms 2636 KB answer = YES
7 Correct 2 ms 2636 KB answer = YES
8 Correct 2 ms 2668 KB answer = YES
9 Correct 2 ms 2636 KB answer = NO
10 Correct 2 ms 2660 KB answer = YES
11 Correct 2 ms 2636 KB answer = YES
12 Correct 2 ms 2636 KB answer = NO
13 Correct 2 ms 2668 KB answer = YES
14 Correct 2 ms 2636 KB answer = YES
15 Correct 2 ms 2764 KB answer = YES
16 Correct 2 ms 2664 KB answer = YES
17 Correct 2 ms 2636 KB answer = YES
18 Correct 2 ms 2636 KB answer = YES
19 Correct 1 ms 2664 KB answer = YES
20 Correct 2 ms 2636 KB answer = YES
21 Correct 2 ms 2636 KB answer = YES
22 Correct 2 ms 2636 KB answer = NO
23 Correct 2 ms 2636 KB answer = NO
24 Correct 2 ms 2652 KB answer = YES
25 Correct 2 ms 2636 KB answer = YES
26 Correct 2 ms 2636 KB answer = YES
27 Correct 2 ms 2636 KB answer = YES
28 Correct 2 ms 2636 KB answer = YES
29 Correct 2 ms 2636 KB answer = YES
30 Correct 2 ms 2636 KB answer = NO
31 Correct 2 ms 2668 KB answer = YES
32 Correct 2 ms 2636 KB answer = YES
33 Correct 2 ms 2648 KB answer = YES
34 Correct 2 ms 2636 KB answer = YES
35 Correct 2 ms 2672 KB answer = YES
36 Correct 2 ms 2636 KB answer = YES
37 Correct 2 ms 2636 KB answer = YES
38 Correct 1 ms 2636 KB answer = YES
39 Correct 2 ms 2636 KB answer = YES
40 Correct 2 ms 2764 KB answer = YES
41 Correct 2 ms 2636 KB answer = NO
42 Correct 2 ms 2764 KB answer = YES
43 Correct 2 ms 2764 KB answer = YES
44 Correct 3 ms 2636 KB answer = YES
45 Correct 3 ms 2676 KB answer = YES
46 Correct 2 ms 2648 KB answer = YES
47 Correct 2 ms 2672 KB answer = YES
48 Correct 2 ms 2764 KB answer = YES
49 Correct 9 ms 3568 KB answer = YES
50 Correct 9 ms 3916 KB answer = YES
51 Correct 11 ms 3956 KB answer = YES
52 Correct 5 ms 3788 KB answer = NO
53 Correct 2 ms 2672 KB answer = YES
54 Correct 3 ms 2800 KB answer = YES
55 Correct 5 ms 3056 KB answer = YES
56 Correct 9 ms 3452 KB answer = YES
57 Correct 8 ms 3404 KB answer = YES
58 Correct 9 ms 3400 KB answer = YES
59 Correct 8 ms 3320 KB answer = YES
60 Correct 9 ms 3404 KB answer = YES
61 Correct 5 ms 3020 KB answer = YES
62 Correct 56 ms 10356 KB answer = NO
63 Correct 67 ms 10388 KB answer = YES
64 Correct 57 ms 10348 KB answer = NO
65 Correct 68 ms 10360 KB answer = YES
66 Correct 3 ms 2764 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2764 KB answer = YES
2 Correct 2 ms 2668 KB answer = YES
3 Correct 2 ms 2636 KB answer = YES
4 Correct 2 ms 2636 KB answer = NO
5 Correct 2 ms 2672 KB answer = YES
6 Correct 2 ms 2636 KB answer = YES
7 Correct 2 ms 2636 KB answer = YES
8 Correct 2 ms 2668 KB answer = YES
9 Correct 2 ms 2636 KB answer = NO
10 Correct 2 ms 2660 KB answer = YES
11 Correct 2 ms 2636 KB answer = YES
12 Correct 2 ms 2636 KB answer = NO
13 Correct 2 ms 2668 KB answer = YES
14 Correct 2 ms 2636 KB answer = YES
15 Correct 2 ms 2764 KB answer = YES
16 Correct 2 ms 2664 KB answer = YES
17 Correct 2 ms 2636 KB answer = YES
18 Correct 2 ms 2636 KB answer = YES
19 Correct 1 ms 2664 KB answer = YES
20 Correct 2 ms 2636 KB answer = YES
21 Correct 2 ms 2636 KB answer = YES
22 Correct 2 ms 2636 KB answer = NO
23 Correct 2 ms 2636 KB answer = NO
24 Correct 2 ms 2652 KB answer = YES
25 Correct 2 ms 2636 KB answer = YES
26 Correct 2 ms 2636 KB answer = YES
27 Correct 2 ms 2636 KB answer = YES
28 Correct 2 ms 2636 KB answer = YES
29 Correct 2 ms 2636 KB answer = YES
30 Correct 2 ms 2636 KB answer = NO
31 Correct 2 ms 2668 KB answer = YES
32 Correct 2 ms 2636 KB answer = YES
33 Correct 2 ms 2648 KB answer = YES
34 Correct 2 ms 2636 KB answer = YES
35 Correct 2 ms 2672 KB answer = YES
36 Correct 2 ms 2636 KB answer = YES
37 Correct 2 ms 2636 KB answer = YES
38 Correct 1 ms 2636 KB answer = YES
39 Correct 2 ms 2636 KB answer = YES
40 Correct 2 ms 2764 KB answer = YES
41 Correct 2 ms 2636 KB answer = NO
42 Correct 2 ms 2764 KB answer = YES
43 Correct 2 ms 2764 KB answer = YES
44 Correct 3 ms 2636 KB answer = YES
45 Correct 3 ms 2676 KB answer = YES
46 Correct 2 ms 2648 KB answer = YES
47 Correct 2 ms 2672 KB answer = YES
48 Correct 2 ms 2764 KB answer = YES
49 Correct 9 ms 3568 KB answer = YES
50 Correct 9 ms 3916 KB answer = YES
51 Correct 11 ms 3956 KB answer = YES
52 Correct 5 ms 3788 KB answer = NO
53 Correct 2 ms 2672 KB answer = YES
54 Correct 3 ms 2800 KB answer = YES
55 Correct 5 ms 3056 KB answer = YES
56 Correct 9 ms 3452 KB answer = YES
57 Correct 8 ms 3404 KB answer = YES
58 Correct 9 ms 3400 KB answer = YES
59 Correct 8 ms 3320 KB answer = YES
60 Correct 9 ms 3404 KB answer = YES
61 Correct 5 ms 3020 KB answer = YES
62 Correct 56 ms 10356 KB answer = NO
63 Correct 67 ms 10388 KB answer = YES
64 Correct 57 ms 10348 KB answer = NO
65 Correct 68 ms 10360 KB answer = YES
66 Correct 3 ms 2764 KB answer = YES
67 Correct 85 ms 18140 KB answer = YES
68 Correct 78 ms 18060 KB answer = YES
69 Correct 71 ms 17968 KB answer = YES
70 Correct 101 ms 22428 KB answer = YES
71 Correct 80 ms 18016 KB answer = YES
72 Correct 90 ms 10812 KB answer = YES
73 Correct 105 ms 10848 KB answer = YES
74 Correct 58 ms 11636 KB answer = YES
75 Correct 25 ms 10936 KB answer = NO
76 Correct 11 ms 3660 KB answer = YES
77 Correct 20 ms 4760 KB answer = YES
78 Correct 36 ms 6216 KB answer = YES
79 Correct 73 ms 9624 KB answer = YES
80 Correct 61 ms 11676 KB answer = YES
81 Correct 40 ms 13372 KB answer = NO
82 Correct 91 ms 17492 KB answer = YES
83 Correct 111 ms 18228 KB answer = YES
84 Correct 146 ms 18176 KB answer = YES
85 Correct 97 ms 18148 KB answer = YES
86 Correct 80 ms 18060 KB answer = YES
87 Correct 53 ms 11652 KB answer = NO
88 Correct 100 ms 13340 KB answer = YES
89 Correct 86 ms 9628 KB answer = YES
90 Correct 84 ms 9644 KB answer = YES
91 Correct 93 ms 9616 KB answer = YES
92 Correct 41 ms 6724 KB answer = YES
93 Correct 52 ms 6832 KB answer = YES
94 Correct 51 ms 17032 KB answer = NO
95 Correct 49 ms 8712 KB answer = NO
96 Correct 159 ms 19304 KB answer = YES
97 Correct 36 ms 17020 KB answer = NO