QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#252251#6396. Puzzle: Kusabiucup-team1074WA 1ms6612kbC++203.1kb2023-11-15 17:13:432023-11-15 17:13:46

Judging History

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

  • [2023-11-15 17:13:46]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:6612kb
  • [2023-11-15 17:13:43]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define x first
#define y second 
#define endl '\n'
const LL maxn = 4e05+7;
const LL N=1e05+10;
const LL mod=1e09+7;
typedef pair<int,int>pl;
priority_queue<LL , vector<LL>, greater<LL> >t;
priority_queue<LL> q;
LL gcd(LL a, LL b){
	return b > 0 ? gcd(b , a % b) : a;
}

LL lcm(LL a , LL b){
	return a / gcd(a , b) * b;
}
int n , m;
int a[N];
void init(int n){
	for(int i = 0 ; i <= n ; i ++){
		a[i] = 0;
	}
}
//TAG = 0 1 2 3
struct Node{
	int tag = 0;
	int depth = 0;
	vector<int>ch;
}tr[N];
vector< pair<int , int> > ans;
int cmp(Node a, Node b){
	return a.depth < b.depth;
}
void dfs(int u , int fa){
	tr[u].depth = tr[fa].depth + 1;
	vector<int>list[4];
	for(auto it : tr[u].ch){
		dfs(it , u);
		list[tr[it].tag].pb(it);
	}
	for(int i = 1 ; i< 4; i ++){
		sort(list[i].begin() , list[i].end());
	}
//tong
	int flag[3] = {0 , 0 , 0} , i , id;
	for(i = 0 ; i + 1 < (int)list[0].size() ;){
		int x = list[0][i] , y = list[0][i + 1];
		if(tr[x].depth == tr[y].depth){
			ans.pb({x , y});
			i += 2;
		}
		else{
			i ++;
			flag[0] ++;
			id = x;
		}
	}
	if(i < list[0].size()){
		id = list[0][i];
		i++;
		flag[0]++;
	}
//1/2
	int len2 = list[2].size() - 1 , len3 = list[3].size() - 1;
	int ss = len2 , tt = len3;
	if(len2 > len3){
		for(i = len2 ; i >= 0; i --){
			while(ss >= 0 && tr[list[2][ss]].depth >= tr[list[3][i]].depth){
				id = list[2][ss];
				ss --;
				flag[1]++;
			}
			if(ss < 0)
				break;
			ans.pb({list[2][ss] , list[3][i]});
			ss--;
		}
		if(ss>=0){
			flag[1] += ss + 1;
			id = list[2][ss];
		}
	}
	else if(len3 > len2){
		int jj = 0;
		for(i = 0 ; i <= ss ; i ++){
			while(jj <= tt && tr[list[2][i]].depth >= tr[list[3][jj]].depth){
				id = list[3][jj];
				jj ++;
				flag[2]++;
			}
			if(jj > tt) break;
			ans.pb({list[2][i] , list[3][jj]});
			jj++;
		}
		if(jj <= tt){
			flag[2] += (tt - jj + 1);
			id = list[3][jj];
		}
	}
	else{
		for(i = 0 ; i <= ss ; i ++){
			if(tr[list[2][i]].depth < tr[list[3][i]].depth){
				ans.pb({list[2][i] , list[3][i]});
			}
			else{
				flag[2] += 2;
			}
		}
	}
	if(flag[0] + flag[1] + flag[2] > 1){
		cout<<"NO\n";
		exit(0);
	}
	if(flag[0]){
		tr[u].tag = 1;
		tr[u].depth = tr[id].depth;
	}
	if(flag[1]){
		tr[u].tag = 2;
		tr[u].depth = tr[id].depth;
	}
	if(flag[2]){
		tr[u].tag = 3;
		tr[u].depth = tr[id].depth;
	}
}
void solve() 
{
	cin >> n;
	for(int i = 2 ; i <= n ; i ++){
		int ch , fa;
		string s;
		cin >> ch >> fa >> s;
		tr[fa].ch.pb(ch);
		if(s == "Tong"){
			tr[i].tag = 1;
		}
		else if(s == "Duan"){
			tr[i].tag = 2;
		}
		else if(s == "Chang"){
			tr[i].tag = 3;
		}
	}
	dfs(1 , 0);
	if(tr[1].tag == 0){
		cout<<"YES\n";
		for(auto it : ans){
			cout<<it.x<<" "<<it.y<<endl;
		}
	}
	else{
		cout<<"NO";
	}
}            
int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(10);
    int t=1;
//	cin>>t;
    while(t--)
    {
    	solve();
    }
    return 0;
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 6612kb

input:

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

output:

YES
6 7
2 3

result:

wrong answer Used vertex 7 is not marked.