Submission #914152

# Submission time Handle Problem Language Result Execution time Memory
914152 2024-01-21T09:49:03 Z mss124 Graph (BOI20_graph) C++14
0 / 100
3 ms 4648 KB
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

/*
	observatii:
		tot sistemul se poate scrie pe o componenta conexa in functie de o singura variabila
		deci toate nodurile se vor putea scrie ca functii f(x) = ax + b
		cu a din {-1, 0, 1} si b din R
		atunci cand un nod are doua functii distince asociate, se poate identifica x / o contradictie

		daca sistemul este (simplu) nedeterminat, trebuie aflat x pt care suma de abs(ax + b) este minima
		conditia se pune ca sum(abs(x - (-b/a))) minima
		functia atinge minimul pt x mediana (-b/a)
			se considera (-b/a)_k <= x <= x + d <= (-b/a)_(k + 1)
			si se studiaza monotonia functiei pe intervale
*/

struct node {

	int index, type;

	node(int a = 0, int t = 0) : index(a), type(t) {}
};

struct function {

	double a, b;

	function(double x = 1, double y = 0) : a(x), b(y) {}
};

const int nmax = 1e5;

vector<node> edges[nmax + 1];
function nodes[nmax + 1];
bool processed[nmax + 1];
bool not_isolated[nmax + 1];

queue<int> dfs_processed;

bool dfs(int curr_node = 1) {

	processed[curr_node] = 1;
	dfs_processed.push(curr_node);

	for (node next : edges[curr_node]) {

		double curr_a = -nodes[curr_node].a,
			   curr_b = next.type - nodes[curr_node].b;

		if (!processed[next.index]) {

			nodes[next.index].a = curr_a;
			nodes[next.index].b = curr_b;

			if (!dfs(next.index))
				return false;
		}
		else {

			if (nodes[next.index].a == curr_a) {

				if (nodes[next.index].b != curr_b)
					return false;
			}
			else {

				double x = (nodes[next.index].b - curr_b) / (curr_a - nodes[next.index].a);

				while (!dfs_processed.empty()) {

					int top = dfs_processed.front();
					dfs_processed.pop();

					nodes[top].b = nodes[top].a * x + nodes[top].b;
					nodes[top].a = 0;
				}
			}
		}	
	}

	return true;
}

bool cmp(const int& x, const int& y) {

	return (-1 * nodes[x].b / nodes[x].a) < (-1 * nodes[y].b / nodes[y].a);
}

int main() {

	int n, m;
	cin >> n >> m;

	for (int i = 1; i <= m; i++) {

		int a, b, t;
		cin >> a >> b >> t;

		not_isolated[a] = not_isolated[b] = 1;

		if (a == b) {

			double curr_b = (double)t / 2;

			if (processed[a] && nodes[a].b != curr_b) {

				cout << "NO";
				return 0;
			}

			nodes[a].a = 0;
			nodes[a].b = curr_b;

			processed[a] = 1;
		}

		bool skip = false;
		for (node next : edges[a]) {

			if (next.index == b) {

				skip = true;

				if (next.type != t) {

					cout << "NO";
					return 0;
				}

				break;
			}
		}

		if (skip)
			continue;

		edges[a].push_back(node(b, t));
		edges[b].push_back(node(a, t));
	}

	for (int i = 1; i <= n; i++) {

		if (processed[i])
			continue;

		if (!not_isolated[i]) {

			processed[i] = 1;
			nodes[i].a = nodes[i].b = 0;

			continue;
		}

		if (!dfs(i)) {

			cout << "NO";
			return 0;
		}

		vector<int> to_sort;
		while (!dfs_processed.empty()) {

			int top = dfs_processed.front();
			dfs_processed.pop();

			if (nodes[top].a)
				to_sort.push_back(top);
		}

		sort(to_sort.begin(), to_sort.end(), cmp);

		int median = (to_sort.size() + 1) / 2;
		double x = -1 * nodes[median - 1].b / nodes[median - 1].a;

		for (int elem : to_sort) {

			nodes[elem].b = nodes[elem].a * x + nodes[elem].b;
			nodes[elem].a = 0;
		}
	}

	cout << "YES\n";
	for (int i = 1; i <= n; i++)
		cout << nodes[i].b << " ";
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4184 KB answer = YES
2 Correct 2 ms 4188 KB answer = YES
3 Correct 2 ms 4188 KB answer = YES
4 Correct 2 ms 4184 KB answer = NO
5 Correct 3 ms 4648 KB answer = YES
6 Incorrect 2 ms 4188 KB participant answer is larger than the answer of jury
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4184 KB answer = YES
2 Correct 2 ms 4188 KB answer = YES
3 Correct 2 ms 4188 KB answer = YES
4 Correct 2 ms 4184 KB answer = NO
5 Correct 3 ms 4648 KB answer = YES
6 Incorrect 2 ms 4188 KB participant answer is larger than the answer of jury
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4184 KB answer = YES
2 Correct 2 ms 4188 KB answer = YES
3 Correct 2 ms 4188 KB answer = YES
4 Correct 2 ms 4184 KB answer = NO
5 Correct 3 ms 4648 KB answer = YES
6 Incorrect 2 ms 4188 KB participant answer is larger than the answer of jury
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4184 KB answer = YES
2 Correct 2 ms 4188 KB answer = YES
3 Correct 2 ms 4188 KB answer = YES
4 Correct 2 ms 4184 KB answer = NO
5 Correct 3 ms 4648 KB answer = YES
6 Incorrect 2 ms 4188 KB participant answer is larger than the answer of jury
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4184 KB answer = YES
2 Correct 2 ms 4188 KB answer = YES
3 Correct 2 ms 4188 KB answer = YES
4 Correct 2 ms 4184 KB answer = NO
5 Correct 3 ms 4648 KB answer = YES
6 Incorrect 2 ms 4188 KB participant answer is larger than the answer of jury
7 Halted 0 ms 0 KB -