「LibreOJ β Round #3」绯色 IOI(抵达)
【题解】
我们可以发现叶子节点的关联点一定是它的父亲节点,那么我们dfs一遍就可以求出所有节点的关联点,或者判断出无解。
对于每个点i,它的关联点u的危险度肯定比它连接的其他点vi的危险度小,我们从u向vi连边。
连边之后我们跑拓扑排序,并且用堆维护当前入度为0的点中编号最小的,以此来让字典序最小。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 #define LL long long 6 #define rg register 7 #define N 1000010 8 using namespace std; 9 int n,tot,cnt,last[N],last2[N],link[N],in[N],q[N],ans[N]; 10 bool vis[N],sol=1; 11 struct edge{int to,pre;}e[N<<1],e2[N<<1]; 12 priority_queue<int,vector<int>,greater<int> >f; 13 char buf[20000010],*ptr=buf-1; 14 inline int read(){ 15 int f=1,k=0; char c=*++ptr; 16 while(c<'0' || c>'9') c=='-'&&(f=-1), c=*++ptr; 17 while(c<='9' && c>='0') k=k*10+c-'0', c=*++ptr; 18 return k*f; 19 } 20 void dfs(int x,int fa){ 21 if(!sol) return; 22 bool ok=0; 23 for(rg int i=last[x],to;i;i=e[i].pre)if((to=e[i].to)!=fa){ 24 dfs(to,x); if(!vis[to]) link[x]=to,vis[to]=1,ok=1; 25 } 26 if(!ok){ 27 if(vis[fa]) sol=0; 28 else link[x]=fa,vis[fa]=1; 29 } 30 } 31 void dfs2(int x,int fa){ 32 int u=link[x]; 33 for(rg int i=last[x],to;i;i=e[i].pre)if((to=e[i].to)!=link[x]){ 34 in[to]++; 35 e2[++tot]=(edge){to,last2[u]}; last2[u]=tot; 36 // printf("%d-->%d\n",u,to); 37 } 38 for(rg int i=last[x],to;i;i=e[i].pre)if((to=e[i].to)!=fa) dfs2(to,x); 39 } 40 int main(){ 41 // freopen("minecraft.in","r",stdin); 42 // freopen("minecraft.out","w",stdout); 43 fread(buf, 1, sizeof(buf), stdin); 44 n=read(); 45 for(rg int i=1;i<n;i++){ 46 int u=read(),v=read(); 47 e[++tot]=(edge){v,last[u]}; last[u]=tot; 48 e[++tot]=(edge){u,last[v]}; last[v]=tot; 49 } 50 vis[0]=1; 51 dfs(1,0); 52 if(!sol){puts("-1"); return 0;} 53 // for(rg int i=1;i<=n;i++) printf("%d ",link[i]); puts("link"); 54 tot=0; 55 dfs2(1,0); 56 // for(rg int i=1;i<=n;i++) printf("%d ",in[i]); puts("in"); 57 for(rg int i=1;i<=n;i++)if(!in[i]){ 58 f.push(i); 59 } 60 while(f.size()){ 61 int now=f.top(); f.pop(); 62 ans[++cnt]=now; 63 for(rg int i=last2[now],to;i;i=e2[i].pre){ 64 in[to=e2[i].to]--; 65 if(!in[to]) f.push(to); 66 } 67 } 68 for(rg int i=1;i<=n;i++) printf("%d ",ans[i]); 69 return 0; 70 }
做法二:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 #define LL long long 6 #define rg register 7 #define N 500010 8 using namespace std; 9 int n,m,tot,ans[N],last[N],deg[N]; 10 bool vis[N],cut[N<<1]; 11 struct edge{ 12 int to,pre; 13 }e[N<<1]; 14 priority_queue<int,vector<int>,greater<int> >q; 15 inline int read(){ 16 int k=0,f=1; char c=getchar(); 17 while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar(); 18 while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar(); 19 return k*f; 20 } 21 int main(){ 22 n=read(); tot=1; 23 if(n==1) return puts("-1"),0; 24 for(rg int i=1;i<n;i++){ 25 int u=read(),v=read(); 26 e[++tot]=(edge){u,last[v]}; last[v]=tot; 27 e[++tot]=(edge){v,last[u]}; last[u]=tot; 28 deg[u]++; deg[v]++; 29 } 30 for(rg int i=1;i<=n;i++)if(deg[i]<=1) q.push(i),vis[i]=1; 31 for(rg int t=1,now;t<=n;t++){ 32 if(!q.size()){puts("-1"); return 0;} 33 ans[t]=now=q.top(); q.pop(); 34 for(rg int i=last[now],to;i;i=e[i].pre)if(!cut[i]){ 35 to=e[i].to; 36 for(rg int j=last[to],to2;j;j=e[j].pre){ 37 deg[to2=e[j].to]--; 38 if(!vis[to2]&°[to2]<=1) q.push(to2),vis[to2]=1; 39 cut[j^1]=1; 40 } 41 } 42 } 43 for(rg int i=1;i<=n;i++) printf("%d ",ans[i]); 44 return 0; 45 }
转载于:https://www.cnblogs.com/DriverLao/p/9883201.html
「LibreOJ β Round #3」绯色 IOI(抵达)相关推荐
- [LOJ #521]「LibreOJ β Round #3」绯色 IOI(抵达)(结论)
#521. 「LibreOJ β Round #3」绯色 IOI(抵达) description solution 因为点的庇护所不能为自身,题目背景在树上,有结论一定是两个相邻点互为庇护所 所以树一 ...
- [LOJ#522]「LibreOJ β Round #3」绯色 IOI(危机)
[LOJ#522]「LibreOJ β Round #3」绯色 IOI(危机) 试题描述 IOI 的比赛开始了.Jsp 和 Rlc 坐在一个角落,这时他们听到了一个异样的声音 -- 接着他们发现自己收 ...
- [匹配+拓扑] LibreOJ #521. 「LibreOJ β Round #3」绯色 IOI(抵达)
题意 戳这里 题解 这题一开始感觉无从下手,所以我们要尝试得到一些结论. 自己画一画可以发现,一定是两两互为对方的庇护所.这样才有可能保证任意两个不同的城市庇护所不同.否则由于原图没有环,到后面必定会 ...
- #520. 「LibreOJ β Round #3」绯色 IOI(开端) 贪心
妈耶,没脸见人了.巨水,想出来不写,人生重来算了. 就是个找规律题,相邻一个连一下,但是我没注意到是IOI赛制,以为是OI赛制所以没打,感觉70分好打但是懒得了.. 证明就是把相邻3个列一下式子就出来 ...
- 【LOJ520】「LibreOJ β Round #3」绯色 IOI(开端)
传送门 题解: 排序. 现在问题是找出两条从111到nnn的路径使得代价最小. 显然路径上点的编号只可能递增. 容易证明iii和i+1i+1i+1不能存在于同一条路径中.考虑p<i<i+1 ...
- 【联合选讲】「LibreOJ β Round #3」绯色 IOI(悬念)
题目 Description 胖头鱼从鱼戏团逃脱后,被主人一路追捕,他慌不择路地跑进了一颗n个节点的池子树,池子树的所有度数为1的点就是出口. 假如他现在在节点i,那么每个时刻他能选择向某个与当前点有 ...
- loj 523 「LibreOJ β Round #3」绯色 IOI(悬念) 霍尔定理+基环树+线段树
题目分析 神仙题(确信) 首先,j−aij-a _ ij−ai和ai−ja _ i-jai−j互为相反数,若其中最小值为bib _ ibi,则一个为bib _ ibi一个为m−bim-b _ ...
- [结论] LibreOJ #520. 「LibreOJ β Round #3」绯色 IOI(开端)
题意 戳这里 题解 这是一道结论题. 我们先把数放到数轴上考虑.定义两个点的距离为几何上的距离的平方. 我们可以把一个回路看作两条从 11 到 nn 的不相交的路径. 有一种经典的二路取数的 O(n2 ...
- 「LibreOJ Round #11」Misaka Network 与求和(杜教筛 + Min_25)
#572. 「LibreOJ Round #11」Misaka Network 与求和 推式子 ∑i=1n∑j=1nf(gcd(i,j))k∑d=1nf(d)k∑i=1nd∑j=1nd[gcd(i,j ...
最新文章
- 校园资源建设平台源代码
- RHTTPHeaders有bug,使用须谨慎
- 申请补办 CET(纸笔考试)成绩证明的方法
- java几种多线程_Java多线程实现的几种方式
- 震惊kafka_5个令人震惊的统计数据证明日志不足
- ASP.net Table 控件
- DEKR 解构式人体关键点回归(二):标签分配(Label Assignment)
- 小米手机,测试时应用图标不能更改,还出现偶尔应用版本还原
- 在reader中勾选pdf复选框_轻松控制PDF文件的打开方式
- 使用反射修改final属性
- Qt安装Windows调试器
- vxworks7.0SD卡驱动流程及文件系统格式化
- IDM:从Google Drive快速直接下载大文件
- SVN:Attempted to lock an already-locked dir svn: Working copy '工作空间路径/lib' locked
- vue使用Echarts绘制自己股票K线图,包含数据接口
- 【vivado使用】第3篇:BD文件(ip integrator)使用技巧
- JSP网上图书商城项目管理系统
- ubuntu:VIM使用
- 技巧:Vimdiff 使用
- Python练习-2
热门文章
- 名片识别sdk助力于商务交流活动
- 写面试题中遇到的问题(18)(被蹂躏篇)
- Node.js 和 AWS S3 服务实现的在线文件存储系统
- 【Angular】技术入门
- iviewUI-Tabs选项卡切换组件
- linux 常用命令手册
- 迁移到ASP.NET:需考虑的重要问题
- 微信小程序——在小程序自定义组件中获取元素定位及宽度
- linux同步机制-complete
- [机械臂笔记001]论文阅读-StructDiffusion: Object-Centric Diffusion for Semantic Rearrangement of Novel Objects