noi.ac NA535 【生成树】

因为太蠢一直写T1也没仔细想,赛后发现是个真小清新思维题,本质构造???

首先显然不会无解,这个随随便便证一下就有了

另外给的式子没啥意义,也就能说明颜色随机???害人不浅

然后就从\(1\)开始,钦点颜色为\(0\),然后顺着编号递增判断能不能同色入栈,不能则弹出栈顶元素,如果弹空了则意味着当前点和其他点都有颜色为\(1\)的边,于是这样跑就能得到解,时间复杂度\(\mathcal{O}(n\log n)\)\(\log\)是因为用了map)

#include<bits/stdc++.h>
#define pii pair<int,int>
#define mk make_pair
#define fi first
#define se second
#define pb push_back
typedef long long ll;

using namespace std;

const int N=5e5+5;

void qread(int &xx){
	xx=0;int ch=getchar();
	while(ch<'0'||ch>'9'){
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		xx=xx*10+ch-'0';
		ch=getchar();
	}
}

void qread(ll &xx){
	xx=0;int ch=getchar();
	while(ch<'0'||ch>'9'){
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		xx=xx*10+ch-'0';
		ch=getchar();
	}
}

map<int,int>G[N];vector<pii>ans[2];stack<int>st;

int n,m,col;long long X,Y,Z,p[N];

int query(int u,int v){
	if(G[u].count(v)){
		return G[u][v];
	}
	return (1LL*(u<v?u:v)*X+1LL*(u>v?u:v)*Y)%Z>=p[u]+p[v];
}

void solve(){
	st.push(1);
	for(int i=2;i<=n;i++){
		while(!st.empty()){
			int u=st.top(),ec=query(u,i);
			ans[ec].pb(mk(u,i));
			if(col^ec){
				st.pop();
			}
			else{
				break;
			}
		}
		if(st.empty()){
			col^=1;st.push(1);
		}
		st.push(i);
	}
}

int main(){
	qread(n);qread(m);
	for(int i=1,u,v,w;i<=m;i++){
		qread(u);qread(v);qread(w);
		G[u][v]=G[v][u]=w;
	}
	qread(X);qread(Y);qread(Z);
	for(int i=1;i<=n;i++){
		qread(p[i]);
	}
	solve();
	for(auto pr:ans[col]){
		printf("%d %d\n",pr.fi,pr.se);
	}
	return 0;
}
posted @ 2019-07-07 00:12  __BLUESKY007  阅读(254)  评论(0编辑  收藏  举报