제출 #1303356

#제출 시각아이디문제언어결과실행 시간메모리
1303356ThanhsGraph (BOI20_graph)C++20
0 / 100
2 ms340 KiB
#include <bits/stdc++.h>
using namespace std;

#define name "TENBAI"
#define fi first
#define se second
#define int long long
#define endl '\n'
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))
#define all(x) x.begin(), x.end()

mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}

const int NM = 2e5 + 5;
const double SUS = -69;

pair<int, int> a[NM], sum;
double force;
pair<double, double> ans;
map<pair<int, int>, int> mp;
int n, m;
bool vis[NM];
vector<pair<int, int>> g[NM];

void dfs(int u)
{
	vis[u] = 1;
	mp[{a[u].fi, a[u].se}]++;
	for (auto t : g[u])
	{
		int v = t.fi, w = t.se;
		if (!vis[v])
		{
			a[v].fi = -a[u].fi, a[v].se = w - a[u].se;
			dfs(v);
		}
		else
		{
			if (a[u].fi + a[v].fi == 0)
			{
				if (a[u].se + a[v].se != w)
				{
					cout << "NO";
					exit(0);
				}
			}
			else
			{
				double t = double(w - (a[u].se + a[v].se)) / (a[u].fi + a[v].fi);
				if (force == SUS)
					force = t;
				else if (force != t)
				{
					cout << "NO";
					exit(0);
				}
			}
		}
	}
}

void solve()
{
	force = -69;
	cin >> n >> m;
	for (int i = 1; i <= m; i++)
	{
		int u, v, w;
		cin >> u >> v >> w;
		g[u].emplace_back(v, w);
		g[v].emplace_back(u, w);
	}
	a[1] = {1, 0};
	dfs(1);
	vector<array<int, 3>> v;
	for (auto t : mp)
		v.push_back({t.fi.fi, t.fi.se, t.se});
	sort(all(v), [&](auto x, auto y) {return -double(x[1]) / x[0] < -double(y[1]) / y[0];});
	if (force != SUS)
		ans.se = force;
	else
	{
		ans.fi = 1e9;
		double pr = -69;
		for (int i = 0; i < v.size(); i++)
		{
			double cr = -double(v[i][1]) / v[i][0];
			// cout << cr << ' ' << SUS << endl;
			pair<int, int> sum;
			for (int j = 0; j < v.size(); j++)
			{
				sum.fi += (j >= i ? -v[j][0] : v[j][0]) * v[j][2];
				sum.se += (j >= i ? -v[j][1] : v[j][1]) * v[j][2];
			}
			for (double x : {pr, cr})
				setmin(ans, make_pair(sum.fi * x + sum.se, x));
			pr = cr;
		}
	}
	cout << "YES\n";
	for (int i = 1; i <= n; i++)
		cout << a[i].fi * ans.se + a[i].se << ' ';
}

signed main()
{
    if (fopen("in.txt", "r")) 
    {
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    }
    else if (fopen(name".inp", "r"))
    {
        freopen(name".inp", "r", stdin);
        freopen(name".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    int tc = 1; 
    // cin >> tc;
    while (tc--)
        solve();
}

컴파일 시 표준 에러 (stderr) 메시지

Graph.cpp: In function 'int main()':
Graph.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |         freopen("in.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Graph.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |         freopen("out.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Graph.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |         freopen(name".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Graph.cpp:118:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |         freopen(name".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...