URAL 1022. Genealogical Tree

2024-08-21 08:18
文章标签 tree ural 1022 genealogical

本文主要是介绍URAL 1022. Genealogical Tree,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


     

本来是写的DFS求最长路的,结果WA at test #2 ,后来发现是因为 图不一定要是连通图。然后就好麻烦了……写了个多次的DFS,没过。

写了个先连成连通图 ,再一次DFS,结果连接成连通图的过程中有错误,WA at  test #5 。


最后发现这题是求 拓扑排序,从头写起,终于过了。

每次删除入度最小的顶点,并输出这个顶点的编号。我写的有点复杂……

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define FOR(i,n) for(long long (i)=1;(i)<=(n);(i)++)
#define For(i,n) for(long long (i)=0;(i)<(n);(i)++)
using namespace std;
struct ArcNode{int from,to;ArcNode *nextOut;//出边表 ArcNode *nextIn;//入边表 
};
struct Node{ArcNode *In;ArcNode *Out;int K;//入度int vis;//0表示已经被删掉。Node():In(NULL),Out(NULL),K(0),vis(0){}bool operator <(const Node &B)const{return K<B.K; } 
}node[101];
void Arc(int from,int to){//建边ArcNode *temp=new(ArcNode);temp->from=from;temp->to=to;//接进入边表 temp->nextIn=node[to].In;node[to].In=temp;node[to].K++;//接进出边表temp->nextOut=node[from].Out;node[from].Out=temp;
}
void DeleNode(int i){//删节点//删除出边 while(node[i].Out){ArcNode *temp=node[i].Out;node[i].Out=node[i].Out->nextOut;node[temp->to].K--;//删除to节点的入边;if(node[temp->to].In==temp){node[temp->to].In=temp->nextIn;}else{ArcNode *temp2=node[temp->to].In;while(temp2){if(temp2->nextIn==temp){temp2->nextIn=temp->nextIn;break;}temp2=temp2->nextIn;}}delete temp;}//删除入边while(node[i].In){ArcNode *temp=node[i].In;node[i].In=node[i].In->nextIn;node[i].K--;//删除from节点的出边;if(node[temp->from].Out==temp){node[temp->from].Out=temp->nextOut;}else{ArcNode *temp2=node[temp->from].Out;while(temp2){if(temp2->nextOut==temp){temp2->nextOut=temp->nextOut;break;}temp2=temp2->nextOut;}}delete temp;}
}
struct A{int i;int K;bool operator<(const A&B)const{return K <B.K; }
}AA[101];
int N;
int main(void)
{while(cin>>N){FOR(i,N) node[i].K=0,node[i].vis=1;//建边 FOR(i,N){int a;scanf("%d",&a);while(a) {Arc(i,a);scanf("%d",&a);}}//计算int ALL=N;while(ALL>0){for(int i=1;i<=N;i++){AA[i].i=i;AA[i].K=node[i].K;}sort(AA+1,AA+N+1);//排序找到入度最小的边 int Cur=1;while(!node[AA[Cur].i].vis) Cur++; DeleNode(AA[Cur].i);node[AA[Cur].i].vis=0;ALL--;if(ALL) printf("%d ",AA[Cur].i);else printf("%d\n",AA[Cur].i);}}
return 0;
}




这篇关于URAL 1022. Genealogical Tree的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1092619

相关文章

ural 1297. Palindrome dp

1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unli

ural 1149. Sinus Dances dfs

1149. Sinus Dances Time limit: 1.0 second Memory limit: 64 MB Let  An = sin(1–sin(2+sin(3–sin(4+…sin( n))…) Let  Sn = (…( A 1+ n) A 2+ n–1) A 3+…+2) An+1 For given  N print  SN Input One

ural 1820. Ural Steaks 贪心

1820. Ural Steaks Time limit: 0.5 second Memory limit: 64 MB After the personal contest, happy but hungry programmers dropped into the restaurant “Ural Steaks” and ordered  n specialty steaks

ural 1017. Staircases DP

1017. Staircases Time limit: 1.0 second Memory limit: 64 MB One curious child has a set of  N little bricks (5 ≤  N ≤ 500). From these bricks he builds different staircases. Staircase consist

ural 1026. Questions and Answers 查询

1026. Questions and Answers Time limit: 2.0 second Memory limit: 64 MB Background The database of the Pentagon contains a top-secret information. We don’t know what the information is — you

ural 1014. Product of Digits贪心

1014. Product of Digits Time limit: 1.0 second Memory limit: 64 MB Your task is to find the minimal positive integer number  Q so that the product of digits of  Q is exactly equal to  N. Inpu

树(Tree)——《啊哈!算法》

树 什么是树?树是一种特殊的图,不包含回路的连通无向树。 正因为树有着“不包含回路”这个特点,所以树就被赋予了很多特性。 一棵树中的任意两个结点有且仅有唯一的一条路径连通。一棵树如果有n个结点,那么它一定恰好有n-1条边。在一棵树中加一条边将会构成一个回路。 一棵树有且只有一个根结点。 没有父结点的结点称为根结点(祖先)。没有子结点的结点称为叶结点。如果一个结点既不是根结点也不是叶

226 Invert Binary Tree

//226 Invert Binary Tree//算法思路:主要使用递归算法public class Solution {public TreeNode invertTree(TreeNode root) {//1 出口 空节点if (root==null)return null;//2 递归 调用自己TreeNode left = root.left;TreeNode right = ro

Sorry!Hbase的LSM Tree就是可以为所欲为!

我们先抛出一个问题: LSM树是HBase里使用的非常有创意的一种数据结构。在有代表性的关系型数据库如MySQL、SQL Server、Oracle中,数据存储与索引的基本结构就是我们耳熟能详的B树和B+树。而在一些主流的NoSQL数据库如HBase、Cassandra、LevelDB、RocksDB中,则是使用日志结构合并树(Log-structured Merge Tree,LSM Tr

【spring】does not have member field ‘com.sun.tools.javac.tree.JCTree qualid

spring-in-action-6-samples 的JDK版本 最小是11,我使用 了22: jdk21 jdk22 都与lombok 不兼容,必须使用兼容版本, 否则报错: thingsboard 的大神解释了: java: java.lang.NoSuchFieldError: Class com