「LibreOJ β Round #3」绯色 IOI(抵达)


【题解】
我们可以发现叶子节点的关联点一定是它的父亲节点,那么我们dfs一遍就可以求出所有节点的关联点,或者判断出无解。
对于每个点i,它的关联点u的危险度肯定比它连接的其他点vi的危险度小,我们从u向vi连边。
连边之后我们跑拓扑排序,并且用堆维护当前入度为0的点中编号最小的,以此来让字典序最小。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define LL long long
#define rg register
#define N 1000010
using namespace std;
int n,tot,cnt,last[N],last2[N],link[N],in[N],q[N],ans[N];
bool vis[N],sol=;
struct edge{int to,pre;}e[N<<],e2[N<<];
priority_queue<int,vector<int>,greater<int> >f;
char buf[],*ptr=buf-;
inline int read(){
int f=,k=; char c=*++ptr;
while(c<'' || c>'') c=='-'&&(f=-), c=*++ptr;
while(c<='' && c>='') k=k*+c-'', c=*++ptr;
return k*f;
}
void dfs(int x,int fa){
if(!sol) return;
bool ok=;
for(rg int i=last[x],to;i;i=e[i].pre)if((to=e[i].to)!=fa){
dfs(to,x); if(!vis[to]) link[x]=to,vis[to]=,ok=;
}
if(!ok){
if(vis[fa]) sol=;
else link[x]=fa,vis[fa]=;
}
}
void dfs2(int x,int fa){
int u=link[x];
for(rg int i=last[x],to;i;i=e[i].pre)if((to=e[i].to)!=link[x]){
in[to]++;
e2[++tot]=(edge){to,last2[u]}; last2[u]=tot;
// printf("%d-->%d\n",u,to);
}
for(rg int i=last[x],to;i;i=e[i].pre)if((to=e[i].to)!=fa) dfs2(to,x);
}
int main(){
// freopen("minecraft.in","r",stdin);
// freopen("minecraft.out","w",stdout);
fread(buf, , sizeof(buf), stdin);
n=read();
for(rg int i=;i<n;i++){
int u=read(),v=read();
e[++tot]=(edge){v,last[u]}; last[u]=tot;
e[++tot]=(edge){u,last[v]}; last[v]=tot;
}
vis[]=;
dfs(,);
if(!sol){puts("-1"); return ;}
// for(rg int i=1;i<=n;i++) printf("%d ",link[i]); puts("link");
tot=;
dfs2(,);
// for(rg int i=1;i<=n;i++) printf("%d ",in[i]); puts("in");
for(rg int i=;i<=n;i++)if(!in[i]){
f.push(i);
}
while(f.size()){
int now=f.top(); f.pop();
ans[++cnt]=now;
for(rg int i=last2[now],to;i;i=e2[i].pre){
in[to=e2[i].to]--;
if(!in[to]) f.push(to);
}
}
for(rg int i=;i<=n;i++) printf("%d ",ans[i]);
return ;
}
做法二:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define LL long long
#define rg register
#define N 500010
using namespace std;
int n,m,tot,ans[N],last[N],deg[N];
bool vis[N],cut[N<<];
struct edge{
int to,pre;
}e[N<<];
priority_queue<int,vector<int>,greater<int> >q;
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
int main(){
n=read(); tot=;
if(n==) return puts("-1"),;
for(rg int i=;i<n;i++){
int u=read(),v=read();
e[++tot]=(edge){u,last[v]}; last[v]=tot;
e[++tot]=(edge){v,last[u]}; last[u]=tot;
deg[u]++; deg[v]++;
}
for(rg int i=;i<=n;i++)if(deg[i]<=) q.push(i),vis[i]=;
for(rg int t=,now;t<=n;t++){
if(!q.size()){puts("-1"); return ;}
ans[t]=now=q.top(); q.pop();
for(rg int i=last[now],to;i;i=e[i].pre)if(!cut[i]){
to=e[i].to;
for(rg int j=last[to],to2;j;j=e[j].pre){
deg[to2=e[j].to]--;
if(!vis[to2]&°[to2]<=) q.push(to2),vis[to2]=;
cut[j^]=;
}
}
}
for(rg int i=;i<=n;i++) printf("%d ",ans[i]);
return ;
}
「LibreOJ β Round #3」绯色 IOI(抵达)的更多相关文章
- [LOJ#522]「LibreOJ β Round #3」绯色 IOI(危机)
[LOJ#522]「LibreOJ β Round #3」绯色 IOI(危机) 试题描述 IOI 的比赛开始了.Jsp 和 Rlc 坐在一个角落,这时他们听到了一个异样的声音 …… 接着他们发现自己收 ...
- loj #547. 「LibreOJ β Round #7」匹配字符串
#547. 「LibreOJ β Round #7」匹配字符串 题目描述 对于一个 01 串(即由字符 0 和 1 组成的字符串)sss,我们称 sss 合法,当且仅当串 sss 的任意一个长度为 ...
- [LOJ#531]「LibreOJ β Round #5」游戏
[LOJ#531]「LibreOJ β Round #5」游戏 试题描述 LCR 三分钟就解决了问题,她自信地输入了结果-- > -- 正在检查程序 -- > -- 检查通过,正在评估智商 ...
- [LOJ#530]「LibreOJ β Round #5」最小倍数
[LOJ#530]「LibreOJ β Round #5」最小倍数 试题描述 第二天,LCR 终于启动了备份存储器,准备上传数据时,却没有找到熟悉的文件资源,取而代之的是而屏幕上显示的一段话: 您的文 ...
- [LOJ#516]「LibreOJ β Round #2」DP 一般看规律
[LOJ#516]「LibreOJ β Round #2」DP 一般看规律 试题描述 给定一个长度为 \(n\) 的序列 \(a\),一共有 \(m\) 个操作. 每次操作的内容为:给定 \(x,y\ ...
- [LOJ#515]「LibreOJ β Round #2」贪心只能过样例
[LOJ#515]「LibreOJ β Round #2」贪心只能过样例 试题描述 一共有 \(n\) 个数,第 \(i\) 个数 \(x_i\) 可以取 \([a_i , b_i]\) 中任意值. ...
- [LOJ#525]「LibreOJ β Round #4」多项式
[LOJ#525]「LibreOJ β Round #4」多项式 试题描述 给定一个正整数 k,你需要寻找一个系数均为 0 到 k−1 之间的非零多项式 f(x),满足对于任意整数 x 均有 f(x) ...
- [LOJ#526]「LibreOJ β Round #4」子集
[LOJ#526]「LibreOJ β Round #4」子集 试题描述 qmqmqm有一个长为 n 的数列 a1,a2,……,an,你需要选择集合{1,2,……,n}的一个子集,使得这个子集中任意两 ...
- LibreOJ #517. 「LibreOJ β Round #2」计算几何瞎暴力
二次联通门 : LibreOJ #517. 「LibreOJ β Round #2」计算几何瞎暴力 /* LibreOJ #517. 「LibreOJ β Round #2」计算几何瞎暴力 叫做计算几 ...
随机推荐
- AtCoder Regular Contest 076 E - Connected?
题目传送门:https://arc076.contest.atcoder.jp/tasks/arc076_c 题目大意: 给定一个\(R×C\)的矩阵,然后给定\(N\)对点,每对点坐标为\((X_{ ...
- Codeforces Round #410 (Div. 2) A
Description Mike has a string s consisting of only lowercase English letters. He wants to change exa ...
- Datapatch AND What to do if the status of a datapatch action was not SUCCESS due to finding non-ignorable errors
1. Enterprise Manager: Starting version 12.1 Enterprise Manager now calls datapatch to complete post ...
- 用 Fleck 实现 websocket 通信
<html lang="en"> <head> <meta charset="utf-8"> <title>rf ...
- D. Tavas and Malekas DFS模拟 + kmp + hash || kmp + hash
http://codeforces.com/contest/535/problem/D 如果真的要把m个串覆盖上一个串上面,是可以得,不会超时. 要注意到一点,全部覆盖后再判断时候合法,和边放边判断, ...
- Suricata的总体架构
Suricata的总体架构 报文检测系统通常四大部分,报文获取.报文解码.报文检测.日志记录:suricata不同的功能安装模块划分,一个模块的输出是另一个模块的输入,suricata通过线程将模块 ...
- 关于HashMap中hash()函数的思考
关于HashMap中hash()函数的思考 JDK7中hash函数的实现 static int hash(int h) { h ^= (h >>> 20) ^ (h >&g ...
- git常用命令图解 & 常见错误
Git 常用命令 基本命令 git clone.这是一种较为简单的初始化方式,当你已经有一个远程的Git版本库,只需要在本地克隆一份 git clone git://github.com/someon ...
- 【转】Android官方架构项目之MVP + Clean
首先,不了解 Clean 架构的可以看看这个,不过也没关系,阅读本文后你也许会对Clean架构思想有一个认识. 对比MVP项目的结构图,我们发现不同之处是新增的这个Domain Layer这层,来隔离 ...
- IOS 根据身份证号码获取 年龄 生日 性别
/** 从身份证上获取年龄 18位身份证 */ -(NSString *)getIdentityCardAge:(NSString *)numberStr { NSDateFormatter *for ...