QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#150734#6396. Puzzle: Kusabiaesthetic#WA 17ms16924kbC++144.9kb2023-08-26 04:35:312023-08-26 04:35:34

Judging History

你现在查看的是最新测评结果

  • [2023-08-26 04:35:34]
  • 评测
  • 测评结果:WA
  • 用时:17ms
  • 内存:16924kb
  • [2023-08-26 04:35:31]
  • 提交

answer

#include "bits/stdc++.h"
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
using namespace std;
// #define int long long
 
#define dbg_loc() cerr << __PRETTY_FUNCTION__ << " : " << __LINE__ << "\n"
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p){ 
	return os << '(' << p.first << ", " << p.second << ')'; 
}
template<typename T_container,typename T=typename enable_if<!is_same<T_container,string>::value, typename T_container::value_type>::type> 
ostream& operator<<(ostream &os, const T_container &v){ 
	os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; 
}
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T){ 
	cerr << ' ' << H; 
	dbg_out(T...); 
}
#define LOCAL
#define LOCAL
#ifdef LOCAL 
#define dbg(...) cerr<<"(" << #__VA_ARGS__<<"):" , dbg_out(__VA_ARGS__) , cerr << endl
#else
#define dbg(...)
#endif
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
ll mod = (1000000007LL);
inline ll Mod(ll a, ll b){return (a%b);}
inline ll poww(ll a, ll b){ll res = 1;while (b > 0){if(b & 1) res = (res * a)%mod;a = (a * a)%mod;b >>= 1;}return res;}
ll gcd (ll a, ll b) { while (b) { a %= b,swap(a, b);}return a;}
void read(vector<int> &w, int n){w.resize(n);for(int i = 0; i < n; i++) cin>>w[i];}
void print(vector<int> &w){for(int i =0; i < sz(w); i++){if(i == sz(w) - 1) cout<<w[i]<<"\n";else cout<<w[i]<<" ";}}
 
///CUIDADO COM MAXN
#define N 100010 // 1E6

int n, k, v[N];
string s;

char val[N];
vi grafo[N];

int h[N];
vector<pii> C[N], D[N], T[N], pares;

void coloca(int x, int v){
	for(auto w: T[v]) T[x].pb(w);
	for(auto w: C[v]) C[x].pb(w);
	for(auto w: D[v]) D[x].pb(w);
}

int melhor_c(int x){
	int ini = 0, fim = sz(C[x]) - 1, mid, best=-1;

	while(fim >= ini){
		int mid = (ini + fim)/2;
		vi novo_c;
		for(int i = 0; i < sz(C[x]); i++){
			if(i != mid) novo_c.pb(C[x][i].f);
		}
		assert(sz(novo_c) == sz(D[x]));
		int can = 1;
		for(int i = 0; i < sz(novo_c); i++){
			if(novo_c[i] <= D[x][i].f) can = 0;
		}
		if(can) best=mid, ini=mid+1;
		else fim=mid-1;
	}
	return best;
}

int melhor_d(int x){
	int ini = 0, fim = sz(D[x]) - 1, mid, best=-1;

	while(fim >= ini){
		int mid = (ini + fim)/2;
		vi novo_d;
		for(int i = 0; i < sz(D[x]); i++){
			if(i != mid) novo_d.pb(D[x][i].f);
		}
		assert(sz(novo_d) == sz(C[x]));
		int can = 1;
		for(int i = 0; i < sz(novo_d); i++){
			if(novo_d[i] >= D[x][i].f) can = 0;
		}
		if(can) best=mid, fim=mid-1;
		else ini=mid+1;
	}
	return best;
}
void dfs(int x, int p){
	// dbg(x);

	if(val[x] == 'T') T[x].pb({h[x], x});
	if(val[x] == 'C') C[x].pb({h[x], x});
	if(val[x] == 'D') D[x].pb({h[x], x});

	for(auto v: grafo[x]){
		if(v==p) continue;
		h[v]=h[x] + 1;

		dfs(v, x);
		coloca(x, v);

		if(sz(T[v]) + sz(C[v]) + sz(D[v]) > 1){
			dbg(v, T[v], C[v], D[v]);
			cout<<"NO\n";
			exit(0);
		}
	}

	// dbg(x, T[x], C[x], D[x]);
	map<int, vi> mapa;
	for(auto w: T[x]) mapa[w.f].pb(w.s);
	vector<pii> novo_T;
	for(auto w: mapa){
		for(int i = 0; i + 1 < sz(w.s); i += 2){
			pares.pb({w.s[i], w.s[i+1]});
		}

		if(sz(w.s) % 2 == 1){
			novo_T.pb({h[w.s.back()], w.s.back()});
		}
	}
	T[x] = novo_T;

	sort(all(C[x])); sort(all(D[x]));
	if(sz(C[x]) == sz(D[x])){
		int can = 1;
		for(int i = 0; i < sz(C[x]); i++){
			if(C[x][i].f <= D[x][i].f) can = 0;
		}
		if(!can){
			cout<<"NO\n";
			exit(0);
		}
		for(int i = 0; i < sz(C[x]); i++){
			pares.pb({C[x][i].s, D[x][i].s});
		}
		C[x].clear(), D[x].clear();
	}
	else if(sz(C[x]) == sz(D[x]) + 1){
		int c = melhor_c(x);
		if(c != -1){
			int i = 0;
			for(int j = 0;j < sz(D[x]); j++){
				if(j == c) i++;
				pares.pb({D[x][j].s, C[x][i].s});
				i++;
			}

			c = C[x][c].s;
			C[x] = {{h[c], c}};
			D[x].clear();
		}
		else{
			cout<<"NO\n";
			exit(0);
		}
	}
	else if(sz(D[x]) == sz(C[x]) + 1){
		int d = melhor_d(x);
		if(d != -1){
			int i = 0;
			for(int j = 0;j < sz(C[x]); j++){
				if(j == d) i++;
				pares.pb({C[x][j].s, D[x][i].s});
				i++;
			}

			d = D[x][d].s;
			D[x] = {{h[d], d}};
			C[x].clear();
		}
		else{
			cout<<"NO\n";
			exit(0);
		}
	}
	else{
		cout<<"NO\n";
		exit(0);
	}

	// dbg("fim", T[x]);

}
int32_t main(){
	ios::sync_with_stdio(false); cin.tie(0);
	cin>>n;
	int qtd[3] = {0};
	for(int i = 1, a, b; i < n; i++){
		string s;
		cin>>a>>b>>s;
		val[a] = s[0];
		grafo[a].pb(b);
		grafo[b].pb(a);
	}
	dfs(1,1);
	if(T[1].empty() and D[1].empty() and C[1].empty()){
		cout<<"YES\n";
		// dbg(sz(pares));
		for(auto p: pares){
			cout<<p.f<<" "<<p.s<<"\n";
		}
	}
	else cout<<"NO\n";
	
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 13408kb

input:

8
2 1 -
3 1 -
4 2 Tong
5 2 Tong
6 3 Duan
7 3 -
8 7 Chang

output:

YES
4 5
8 6

result:

ok Correct.

Test #2:

score: 0
Accepted
time: 1ms
memory: 13528kb

input:

10
2 1 Duan
3 2 Duan
4 2 -
5 4 Chang
6 2 Chang
7 1 Duan
8 6 Tong
9 6 Tong
10 3 Chang

output:

YES
10 3
8 9
2 6
5 7

result:

ok Correct.

Test #3:

score: 0
Accepted
time: 3ms
memory: 13592kb

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

score: -100
Wrong Answer
time: 17ms
memory: 16924kb

input:

100000
2 1 Duan
3 1 Duan
4 3 -
5 4 Duan
6 3 -
7 4 Duan
8 4 -
9 8 -
10 7 Duan
11 9 -
12 7 Duan
13 7 Duan
14 8 Duan
15 13 -
16 10 Duan
17 11 Duan
18 12 -
19 1 Duan
20 5 Duan
21 4 Duan
22 14 Duan
23 16 -
24 22 Duan
25 16 Duan
26 13 -
27 13 -
28 17 -
29 5 Duan
30 22 -
31 23 -
32 9 Duan
33 5 -
34 30 Duan...

output:

NO

result:

wrong answer Jury has answer but participant has not.