E 题过了 但是感觉怪怪的 有没有大佬看下正确性

https://ac.nowcoder.com/acm/contest/view-submission?submissionId=73044663

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <string.h>
using namespace std;
// int mod = 998244353;
const int mod = 998244353;
const int modd = 1e9 + 7;
#define ll long long
#define db double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<db, db>
#define vct vector
#define vi vct<int>
#define vl vct<ll>
#define str string
#define mset multiset
#define pq priority_queue
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define of(i, l, r) for (int i = l; i >= r; i--)
#define fi first
#define se second
#define in insert
#define er erase
#define ct count
#define pb push_back
#define pf push_front
#define lowb lower_bound
#define upb upper_bound
#define sz size()
#define cl clear()
#define em empty()
#define be begin()
#define en end()
#define fr front()
#define bk back()
#define pp pop()
#define ppb pop_back()
#define ppf pop_front()
#define S s = " " + s
#define SS s[i] = " " + s[i]
#define all(g) g.be, g.en
#define mem(a, x) memset(a, x, sizeof(a))
#define sp(x) cout << fixed << setprecision(x)
#define M(x) x %= mod;if(x < 0) x += mod
#define ANS cout << ans << '\n'
#define YES cout << "YES\n"
#define Yes cout << "Yes\n"
#define NO cout << "NO\n"
#define No cout << "No\n"
#define rty return
#define ind " \n"[i == n]
#define jnd " \n"[j == n]
#define knd " \n"[k == n]
#define imd " \n"[i == m]
#define jmd " \n"[j == m]
#define kmd " \n"[k == m]
#define GG g[u].pb(v), g[v].pb(u)
#define GGG g[u].pb({v, w}), g[v].pb({u, w})
#define cmx(x, y) x = max(x, y)
#define cmi(x, y) x = min(x, y)
int bg = 1;
#define print(x...) if(bg) cout << #x << "=", O(x);
void O()
{
	cout << '\n';
}
template<class A, class...B>
void O (A a, B...b)
{
	cout << a << ',';
	O (b...);
}
const db pi = acos (-1.0);
// ax+by=(+-)g
// x=x0+b/g*k
// y=y0-a/g*k
ll exgcd (ll a, ll &x, ll b, ll &y)
{
	if (b == 0)
	{
		x = 1, y = 0;
		rty a;
	}
	ll tt = exgcd (b, y, a % b, x);
	y -= a / b * x;
	rty tt;
}
ll gcd (ll a, ll b)
{
	rty b ? gcd (b, a % b) : a;
}
ll qp (ll a, ll b)
{
	ll s = 1;
	while (b > 0)
	{
		if (b & 1)
			s = (s * a) % mod;
		b >>= 1, a = (a * a) % mod;
	}
	rty s;
}
ll ny (ll a)
{
	// ll x, y;
	// if (exgcd(a, x, P, y) == 1)
	//     rty (x % P + P) % P;
	// else
	//     rty -1;
	rty qp (a, mod - 2);
}
//const int X = 1e6 + 5;
//ll fc[X], nn[X], ifc[X];
//void CC()
//{
//	fc[0] = nn[0] = ifc[0] = 1;
//	fo (i, 1, X - 2) fc[i] = fc[i - 1] * i % mod;
//	ifc[X - 2] = ny (fc[X - 2]);
//	of (i, X - 2 - 1, 1) ifc[i] = ifc[i + 1] * (i + 1) % mod;
//	fo (i, 1, X - 2) nn[i] = ifc[i] * fc[i - 1] % mod;
//}
//ll C (int n, int m)
//{
//	if (n < m || n < 0 || m < 0)
//		rty 0;
//	rty fc[n] * ifc[m] % mod * ifc[n - m] % mod;
//}
int dx[5] = {0, 1, 0, -1, 0};
int dy[5] = {0, 0, 1, 0, -1};
ll MAX = 0x3f3f3f3f3f3f3f3fll;
ll MIN = -MAX;
ll t, n, m, k, x, y, z, u, v, w, l, r, mid, tt, qq, ff, ok, T, mx, mi, op, pos, len, sum, ans;

void CL()
{
	ans = sum = len = ff = tt = ok = x = y = z = n = m = k = t = 0, mi = MAX, mx = MIN;
	rty;
}
const int N = 1e6 + 5;
ll a[N];
ll b[N];
ll d1[N];
ll d2[N];
vi g1[N];
vi g2[N];
pll q[N];
void solve()
{
	cin >> n;
	fo (i, 1, n)
	{
		cin >> m >> x;
		g1[i].pb (x);
		g2[x].pb (i);
		d1[i]++;
		d2[x]++;
		if (m == 2)
		{
			cin >> y;
			if (x != y)
			{
				x = y;
				g1[i].pb (x);
				g2[x].pb (i);
				d1[i]++;
				d2[x]++;
			}
		}
	}
	int D = 0;
	fo (i, 1, n)
	{
		if (d1[i] == 1)
			q[++D] = {i, 1};

		if (d2[i] == 1)
			q[++D] = {i, 2};
	}
	fo (i, 1, D)
	{
		auto[u, op] = q[i];
		if (op == 1)
		{
			for (auto v : g1[u])
			{
				if (!a[u] && !b[v])
				{
					a[u] = v;
					b[v] = u;
				}
				if (--d2[v] == 1)
					q[++D] = {v, 2};
			}
		}
		else
		{
			for (auto v : g2[u])
			{
				if (!a[v] && !b[u])
				{
					a[v] = u;
					b[u] = v;
				}
				if (--d1[v] == 1)
					q[++D] = {v, 1};
			}
		}
	}
	fo (i, 1, n)
	{
		if (!a[i])
		{
			for (auto it : g1[i])
			{
				if (!b[it])
				{
					a[i] = it;
					b[it] = i;
					break;
				}
			}
		}
		if (!a[i]) ff = 1;
	}
	if (ff)
		cout << "kou is angry";
	else
		fo (i, 1, n) cout << a[i] << ind;
	rty;
}

signed main()
{
	// bg = 0;
	// CC();
	// shai();
	// init();
	ios::sync_with_stdio (0), cin.tie (0), cout.tie (0);
	sp (12);
	// cin >> T; while (T--)
	{
		CL();
		solve();
	}
	rty 0;
}

全部评论
《E 题过了》
点赞 回复 分享
发布于 2024-11-09 09:29 广东

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务