답안 #783675

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
783675 2023-07-15T08:16:35 Z raysh07 Graph (BOI20_graph) C++17
0 / 100
2 ms 2644 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

const int N = 1e5 + 69;
vector <pair<int, int>> adj[N];
double ans[N];
int c[N], d[N];
bool vis[N];
int n, m;
vector <double> forced;
vector <int> comp;

void equate(int c1, int d1, int c2, int d2){
	if (c1 == c2){
		if (d1 == d2);
		else forced.push_back(0), forced.push_back(1);
	} else {
		assert(c1 + c2 == 0);
		//c1x + d1 = c2x + d2
		//(c1 - c2) x = d2 - d1
		// x = (d2 - d1) / (c1 - c2) 
		forced.push_back((double)(d2 - d1) / (c1 - c2));
	}
}

void dfs(int u){
	vis[u] = true;
	comp.push_back(u);
	for (auto [v, w] : adj[u]){
		if (vis[v]){
			equate(c[v], d[v], -c[u], w - d[u]);
		} else {
			c[v] = -c[u];
			d[v] = -d[u];
			d[v] += w;
			dfs(v);
		}
	}
}

void Solve()
{
	cin >> n >> m;

	while (m--){
		int u, v, c; cin >> u >> v >> c;

		adj[u].push_back({v, c});
		adj[v].push_back({u, c});
	}

	for (int i = 1; i <= n; i++){
		if (vis[i]) continue;

		comp.clear();
		forced.clear();

		c[i] = 1;
		d[i] = 0;

		dfs(i);

		sort(forced.begin(), forced.end());
		for (int i = 1; i < forced.size(); i++){
			if (forced[i] != forced[i - 1]){
				cout << "NO\n";
				return;
			}
		}

		if (forced.size() == 0){
			vector <int> f;
			for (auto x : comp){
				f.push_back(d[x] * c[x]);
			}

			sort(f.begin(), f.end());
			forced.push_back(-f[(int)f.size() / 2]);
		}

		int val = forced[0];
		for (auto x : comp){
			ans[x] = val * c[x] + d[x];
		}
	}
	cout << "YES\n";
	cout << fixed << setprecision(10);
	for (int i = 1; i <= n; i++){
		cout << ans[i] << " \n"[i == n];
	}
}

int32_t main() 
{
    auto begin = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;

   // freopen("in",  "r", stdin);
   // freopen("out", "w", stdout);

  //  cin >> t;
    for(int i = 1; i <= t; i++) 
    {
        //cout << "Case #" << i << ": ";
        Solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
    return 0;
}

Compilation message

Graph.cpp: In function 'void Solve()':
Graph.cpp:70:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<double>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |   for (int i = 1; i < forced.size(); i++){
      |                   ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2644 KB Sum of endpoints for edge (1; 3) differs from the expected value 2.
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2644 KB Sum of endpoints for edge (1; 3) differs from the expected value 2.
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2644 KB Sum of endpoints for edge (1; 3) differs from the expected value 2.
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2644 KB Sum of endpoints for edge (1; 3) differs from the expected value 2.
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2644 KB Sum of endpoints for edge (1; 3) differs from the expected value 2.
2 Halted 0 ms 0 KB -