Submission #827935

# Submission time Handle Problem Language Result Execution time Memory
827935 2023-08-16T23:12:12 Z MohamedAhmed04 Graph (BOI20_graph) C++14
100 / 100
108 ms 23236 KB
#include <bits/stdc++.h>

using namespace std ;

const int MAX = 1e5 + 10 ;

int arr[MAX] ;
int n , m ;

vector< vector< pair<int , int> > >adj(MAX) ;

int vis[MAX] ;
int a[MAX] , b[MAX] ;

int known = -1 ;
bool flag = true ;

int ans[MAX] ;

vector<int>v ;

void dfs(int node)
{
	vis[node] = 1 ;
	v.push_back(a[node] * (-1 * b[node])) ;
	for(auto &childd : adj[node])
	{
		int child = childd.first , w = childd.second ;
		if(vis[child])
		{
			if(b[node] + b[child] != 0)
			{
				int x = (w - a[node] - a[child]) / (b[node] + b[child]) ;
				ans[node] = a[node] + x * b[node] ;
				known = node ;
			}
			else if(a[node] + a[child] != w)
				flag = false ;
			continue ;
		}
		a[child] = w - a[node] , b[child] = -1 * b[node] ;
		dfs(child) ;
	}
}

void dfs2(int node)
{
	vis[node] = 2 ;
	for(auto &childd : adj[node])
	{
		int child = childd.first , w = childd.second ;
		if(vis[child] == 2)
		{
			if(ans[node] + ans[child] != w)
				flag = false ;
			continue ;
		}
		ans[child] = w - ans[node] ;
		dfs2(child) ;
	}
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n>>m ;
	for(int i = 0 ; i < m ; ++i)
	{
		int x , y , z ;
		cin>>x>>y>>z ;
		z *= 2 ;
		adj[x].emplace_back(y , z) ;
		adj[y].emplace_back(x , z) ;
	}
	for(int i = 1 ; i <= n ; ++i)
	{
		if(vis[i])
			continue ;
		v.clear() , known = -1 ;
		b[i] = 1 ;
		dfs(i) ;
		if(!flag)
			return cout<<"NO\n" , 0 ;
		if(known == -1)
		{
			sort(v.begin() , v.end()) ;
			ans[i] = v[v.size()/2] ;
			known = i ;
		}
		dfs2(known) ;
		if(!flag)
			return cout<<"NO\n" , 0 ;
	}
	cout<<"YES\n" ;
	for(int i = 1 ; i <= n ; ++i)
		cout<<ans[i] / 2.00<<" " ;
	cout<<"\n" ;
	return 0 ;
}		
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 1 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 1 ms 2644 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 1 ms 2700 KB answer = NO
10 Correct 1 ms 2644 KB answer = YES
11 Correct 1 ms 2644 KB answer = YES
12 Correct 2 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 1 ms 2644 KB answer = YES
15 Correct 2 ms 2772 KB answer = YES
16 Correct 1 ms 2700 KB answer = YES
17 Correct 1 ms 2644 KB answer = YES
18 Correct 2 ms 2692 KB answer = YES
19 Correct 2 ms 2700 KB answer = YES
20 Correct 2 ms 2644 KB answer = YES
21 Correct 1 ms 2644 KB answer = YES
22 Correct 1 ms 2644 KB answer = NO
23 Correct 2 ms 2644 KB answer = NO
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 1 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 1 ms 2644 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 1 ms 2700 KB answer = NO
10 Correct 1 ms 2644 KB answer = YES
11 Correct 1 ms 2644 KB answer = YES
12 Correct 2 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 1 ms 2644 KB answer = YES
15 Correct 2 ms 2772 KB answer = YES
16 Correct 1 ms 2700 KB answer = YES
17 Correct 1 ms 2644 KB answer = YES
18 Correct 2 ms 2692 KB answer = YES
19 Correct 2 ms 2700 KB answer = YES
20 Correct 2 ms 2644 KB answer = YES
21 Correct 1 ms 2644 KB answer = YES
22 Correct 1 ms 2644 KB answer = NO
23 Correct 2 ms 2644 KB answer = NO
24 Correct 1 ms 2644 KB answer = YES
25 Correct 1 ms 2700 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 2 ms 2700 KB answer = YES
28 Correct 2 ms 2644 KB answer = YES
29 Correct 2 ms 2692 KB answer = YES
30 Correct 2 ms 2644 KB answer = NO
31 Correct 1 ms 2644 KB answer = YES
32 Correct 1 ms 2644 KB answer = YES
33 Correct 2 ms 2644 KB answer = YES
34 Correct 1 ms 2644 KB answer = YES
35 Correct 2 ms 2644 KB answer = YES
36 Correct 1 ms 2644 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 1 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 1 ms 2644 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 1 ms 2700 KB answer = NO
10 Correct 1 ms 2644 KB answer = YES
11 Correct 1 ms 2644 KB answer = YES
12 Correct 2 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 1 ms 2644 KB answer = YES
15 Correct 2 ms 2772 KB answer = YES
16 Correct 1 ms 2700 KB answer = YES
17 Correct 1 ms 2644 KB answer = YES
18 Correct 2 ms 2692 KB answer = YES
19 Correct 2 ms 2700 KB answer = YES
20 Correct 2 ms 2644 KB answer = YES
21 Correct 1 ms 2644 KB answer = YES
22 Correct 1 ms 2644 KB answer = NO
23 Correct 2 ms 2644 KB answer = NO
24 Correct 1 ms 2644 KB answer = YES
25 Correct 1 ms 2700 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 2 ms 2700 KB answer = YES
28 Correct 2 ms 2644 KB answer = YES
29 Correct 2 ms 2692 KB answer = YES
30 Correct 2 ms 2644 KB answer = NO
31 Correct 1 ms 2644 KB answer = YES
32 Correct 1 ms 2644 KB answer = YES
33 Correct 2 ms 2644 KB answer = YES
34 Correct 1 ms 2644 KB answer = YES
35 Correct 2 ms 2644 KB answer = YES
36 Correct 1 ms 2644 KB answer = YES
37 Correct 2 ms 2644 KB answer = YES
38 Correct 2 ms 2696 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 2 ms 2772 KB answer = YES
43 Correct 2 ms 2772 KB answer = YES
44 Correct 2 ms 2704 KB answer = YES
45 Correct 2 ms 2772 KB answer = YES
46 Correct 2 ms 2644 KB answer = YES
47 Correct 2 ms 2704 KB answer = YES
48 Correct 2 ms 2772 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 1 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 1 ms 2644 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 1 ms 2700 KB answer = NO
10 Correct 1 ms 2644 KB answer = YES
11 Correct 1 ms 2644 KB answer = YES
12 Correct 2 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 1 ms 2644 KB answer = YES
15 Correct 2 ms 2772 KB answer = YES
16 Correct 1 ms 2700 KB answer = YES
17 Correct 1 ms 2644 KB answer = YES
18 Correct 2 ms 2692 KB answer = YES
19 Correct 2 ms 2700 KB answer = YES
20 Correct 2 ms 2644 KB answer = YES
21 Correct 1 ms 2644 KB answer = YES
22 Correct 1 ms 2644 KB answer = NO
23 Correct 2 ms 2644 KB answer = NO
24 Correct 1 ms 2644 KB answer = YES
25 Correct 1 ms 2700 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 2 ms 2700 KB answer = YES
28 Correct 2 ms 2644 KB answer = YES
29 Correct 2 ms 2692 KB answer = YES
30 Correct 2 ms 2644 KB answer = NO
31 Correct 1 ms 2644 KB answer = YES
32 Correct 1 ms 2644 KB answer = YES
33 Correct 2 ms 2644 KB answer = YES
34 Correct 1 ms 2644 KB answer = YES
35 Correct 2 ms 2644 KB answer = YES
36 Correct 1 ms 2644 KB answer = YES
37 Correct 2 ms 2644 KB answer = YES
38 Correct 2 ms 2696 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 2 ms 2772 KB answer = YES
43 Correct 2 ms 2772 KB answer = YES
44 Correct 2 ms 2704 KB answer = YES
45 Correct 2 ms 2772 KB answer = YES
46 Correct 2 ms 2644 KB answer = YES
47 Correct 2 ms 2704 KB answer = YES
48 Correct 2 ms 2772 KB answer = YES
49 Correct 9 ms 3472 KB answer = YES
50 Correct 8 ms 3924 KB answer = YES
51 Correct 9 ms 3940 KB answer = YES
52 Correct 5 ms 3796 KB answer = NO
53 Correct 2 ms 2772 KB answer = YES
54 Correct 2 ms 2772 KB answer = YES
55 Correct 4 ms 3028 KB answer = YES
56 Correct 7 ms 3344 KB answer = YES
57 Correct 7 ms 3284 KB answer = YES
58 Correct 6 ms 3352 KB answer = YES
59 Correct 7 ms 3284 KB answer = YES
60 Correct 7 ms 3412 KB answer = YES
61 Correct 5 ms 3100 KB answer = YES
62 Correct 43 ms 10440 KB answer = NO
63 Correct 45 ms 10536 KB answer = YES
64 Correct 42 ms 10420 KB answer = NO
65 Correct 44 ms 10496 KB answer = YES
66 Correct 3 ms 2772 KB answer = YES
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB answer = YES
2 Correct 2 ms 2644 KB answer = YES
3 Correct 1 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 1 ms 2644 KB answer = YES
8 Correct 1 ms 2644 KB answer = YES
9 Correct 1 ms 2700 KB answer = NO
10 Correct 1 ms 2644 KB answer = YES
11 Correct 1 ms 2644 KB answer = YES
12 Correct 2 ms 2644 KB answer = NO
13 Correct 2 ms 2644 KB answer = YES
14 Correct 1 ms 2644 KB answer = YES
15 Correct 2 ms 2772 KB answer = YES
16 Correct 1 ms 2700 KB answer = YES
17 Correct 1 ms 2644 KB answer = YES
18 Correct 2 ms 2692 KB answer = YES
19 Correct 2 ms 2700 KB answer = YES
20 Correct 2 ms 2644 KB answer = YES
21 Correct 1 ms 2644 KB answer = YES
22 Correct 1 ms 2644 KB answer = NO
23 Correct 2 ms 2644 KB answer = NO
24 Correct 1 ms 2644 KB answer = YES
25 Correct 1 ms 2700 KB answer = YES
26 Correct 2 ms 2644 KB answer = YES
27 Correct 2 ms 2700 KB answer = YES
28 Correct 2 ms 2644 KB answer = YES
29 Correct 2 ms 2692 KB answer = YES
30 Correct 2 ms 2644 KB answer = NO
31 Correct 1 ms 2644 KB answer = YES
32 Correct 1 ms 2644 KB answer = YES
33 Correct 2 ms 2644 KB answer = YES
34 Correct 1 ms 2644 KB answer = YES
35 Correct 2 ms 2644 KB answer = YES
36 Correct 1 ms 2644 KB answer = YES
37 Correct 2 ms 2644 KB answer = YES
38 Correct 2 ms 2696 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 2 ms 2772 KB answer = YES
43 Correct 2 ms 2772 KB answer = YES
44 Correct 2 ms 2704 KB answer = YES
45 Correct 2 ms 2772 KB answer = YES
46 Correct 2 ms 2644 KB answer = YES
47 Correct 2 ms 2704 KB answer = YES
48 Correct 2 ms 2772 KB answer = YES
49 Correct 9 ms 3472 KB answer = YES
50 Correct 8 ms 3924 KB answer = YES
51 Correct 9 ms 3940 KB answer = YES
52 Correct 5 ms 3796 KB answer = NO
53 Correct 2 ms 2772 KB answer = YES
54 Correct 2 ms 2772 KB answer = YES
55 Correct 4 ms 3028 KB answer = YES
56 Correct 7 ms 3344 KB answer = YES
57 Correct 7 ms 3284 KB answer = YES
58 Correct 6 ms 3352 KB answer = YES
59 Correct 7 ms 3284 KB answer = YES
60 Correct 7 ms 3412 KB answer = YES
61 Correct 5 ms 3100 KB answer = YES
62 Correct 43 ms 10440 KB answer = NO
63 Correct 45 ms 10536 KB answer = YES
64 Correct 42 ms 10420 KB answer = NO
65 Correct 44 ms 10496 KB answer = YES
66 Correct 3 ms 2772 KB answer = YES
67 Correct 70 ms 18952 KB answer = YES
68 Correct 59 ms 18768 KB answer = YES
69 Correct 66 ms 18828 KB answer = YES
70 Correct 83 ms 23236 KB answer = YES
71 Correct 69 ms 19028 KB answer = YES
72 Correct 64 ms 10044 KB answer = YES
73 Correct 66 ms 9988 KB answer = YES
74 Correct 53 ms 11940 KB answer = YES
75 Correct 19 ms 11456 KB answer = NO
76 Correct 9 ms 3540 KB answer = YES
77 Correct 16 ms 4564 KB answer = YES
78 Correct 27 ms 5884 KB answer = YES
79 Correct 55 ms 8952 KB answer = YES
80 Correct 44 ms 11976 KB answer = YES
81 Correct 30 ms 13696 KB answer = NO
82 Correct 93 ms 18328 KB answer = YES
83 Correct 78 ms 18900 KB answer = YES
84 Correct 76 ms 18868 KB answer = YES
85 Correct 63 ms 18884 KB answer = YES
86 Correct 62 ms 18864 KB answer = YES
87 Correct 31 ms 11592 KB answer = NO
88 Correct 75 ms 13360 KB answer = YES
89 Correct 61 ms 8976 KB answer = YES
90 Correct 63 ms 9000 KB answer = YES
91 Correct 63 ms 9008 KB answer = YES
92 Correct 32 ms 6480 KB answer = YES
93 Correct 31 ms 6356 KB answer = YES
94 Correct 39 ms 18228 KB answer = NO
95 Correct 26 ms 8628 KB answer = NO
96 Correct 108 ms 19556 KB answer = YES
97 Correct 28 ms 18116 KB answer = NO