G - Genealogy Gym - 100519G(有环图拓扑排序)

2024-04-16 01:18

本文主要是介绍G - Genealogy Gym - 100519G(有环图拓扑排序),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

As you may know, the dates in historical documents and descriptions can be given rather inaccurately. It often occurs that in a description of a historic event, the closest thing to a date is something like “In the times of Isaac, the son of Abraham”.

The professor Mofenko who is an admirer of alternative history wants to determine the maximum length of the chain of people from father to son that can be composed of people from a given list. With the help of this information, he can invent a lot of amusing alternative histories that are irrefutable by documents.

For each person in the list, his family name, his first name and the first name of his father are given. The family names of a father and his son are always equal. By a happy coincidence, it turned out that all first names of all people in the list are distinct.

Input
The first line of input contains an integer N: the number of the descriptions of people (1 ≤ N ≤ 105).

The next N lines contain the description of a man in the following format: the family name, the first name, the words “son of” and the first name of his father.

All first names and family names start with a capital English letter, and all letters except the first one are lowercase English letters. Consecutive words are separated by a single space character. The length of each first name and each family name is not greater than 10.

Note that it is possible that some person in the list has father outside of the list, but this father shouldn’t be included in the chain. It is guaranteed that all N first names of people in the list are pairwise distinct; however, this restriction may not affect first names of people outside of the list.

Output
Output a single integer: the number of people in the longest chain.

Examples
Input
7
Bible Isaak son of Abraham
Bible Jacob son of Isaak
Ivanov Ivan son of Petr
Ivanov Protopop son of Vasiliy
Ivanov Vasiliy son of Ivan
Ivanov Petr son of Vasiliy
Bible Judah son of Jacob
Output
4
Input
3
Ivanov Ivan son of Petr
Ivanov Petr son of Vasiliy
Petrov Vasiliy son of Ivan
Output
2
Note
In the first example, the longest chain is: Ivanov Petr, Ivanov Ivan, Ivanov Vasiliy, Ivanov Protopop.
In the second example, the longest chain is: Ivanov Petr, Ivanov Ivan.

两场比赛都出了topo,都没写出来。。。
题意:
n n n个人,给出每个人的父亲。每个人只有一个父亲,可以有多个儿子。且图有环,求图上最长链。

思路:
这个环看的我很迷,当然这不是重点。假设你按照儿子连接父亲建图,那么对于每一个环只可能有入度不可能有出度。那么环相当于缩点了。你不考虑环,那就变成了一个森林。topo排序取更新每个节点为结尾的最长链,最后特判环。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <string>
#include <iostream>
#include <cmath>using namespace std;
typedef long long ll;const ll maxn = 1e5 + 7;int deg[maxn],ans,vis[maxn],f[maxn];
map<pair<string,string>,int>mp;
int n;struct Node {string family;string name;string father;
}a[maxn];int head[maxn],nex[maxn],to[maxn],tot;void add(int x,int y) {to[++tot] = y;nex[tot] = head[x];head[x] = tot;
}void topo() {queue<int>q;for(int i = 1;i <= n;i++) {if(deg[i] == 0) {q.push(i);}}while(!q.empty()) {int x = q.front();q.pop();vis[x] = 1;for(int i = head[x];i;i = nex[i]) {int y = to[i];f[y] = f[x] + 1;ans = max(ans,f[y]);deg[y]--;if(deg[y] == 0) {q.push(y);}}}
}int num;
int dfs(int x) {int cnt = 1;num = max(num,f[x]);for(int i = head[x];i;i = nex[i]) {int v = to[i];if(vis[v]) continue;vis[v] = 1;cnt += dfs(v);}return cnt;
}int main() {cin >> n;for(int i = 1;i <= n;i++) {f[i] = 1;string s1,s2,s3,s4,s5;cin >> s1 >> s2 >> s3 >> s4 >> s5;mp[{s1,s2}] = i;a[i].family = s1;a[i].name = s2;a[i].father = s5;}ans = 1;for(int i = 1;i <= n;i++) {string s1 = a[i].family,s2 = a[i].name,s3 = a[i].father;int num1 = mp[{s1,s3}]; //老子int num2 = mp[{s1,s2}]; //儿子if(num1 == 0) continue;add(num2,num1);
//        printf("%d %d\n",num1,num2);deg[num1]++;}topo();for(int i = 1;i <= n;i++) {if(!vis[i]) {num = 0;int circle = dfs(i) - 1;ans = max(ans,circle + num - 1);}}printf("%d\n",ans);return 0;
}

这篇关于G - Genealogy Gym - 100519G(有环图拓扑排序)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

大数据小内存排序问题如何巧妙解决

《大数据小内存排序问题如何巧妙解决》文章介绍了大数据小内存排序的三种方法:数据库排序、分治法和位图法,数据库排序简单但速度慢,对设备要求高;分治法高效但实现复杂;位图法可读性差,但存储空间受限... 目录三种方法:方法概要数据库排序(http://www.chinasem.cn对数据库设备要求较高)分治法(常

Python中lambda排序的六种方法

《Python中lambda排序的六种方法》本文主要介绍了Python中使用lambda函数进行排序的六种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录1.对单个变量进行排序2. 对多个变量进行排序3. 降序排列4. 单独降序1.对单个变量进行排序

关于Java内存访问重排序的研究

《关于Java内存访问重排序的研究》文章主要介绍了重排序现象及其在多线程编程中的影响,包括内存可见性问题和Java内存模型中对重排序的规则... 目录什么是重排序重排序图解重排序实验as-if-serial语义内存访问重排序与内存可见性内存访问重排序与Java内存模型重排序示意表内存屏障内存屏障示意表Int

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

hdu 1285(拓扑排序)

题意: 给各个队间的胜负关系,让排名次,名词相同按从小到大排。 解析: 拓扑排序是应用于有向无回路图(Direct Acyclic Graph,简称DAG)上的一种排序方式,对一个有向无回路图进行拓扑排序后,所有的顶点形成一个序列,对所有边(u,v),满足u 在v 的前面。该序列说明了顶点表示的事件或状态发生的整体顺序。比较经典的是在工程活动上,某些工程完成后,另一些工程才能继续,此时

《数据结构(C语言版)第二版》第八章-排序(8.3-交换排序、8.4-选择排序)

8.3 交换排序 8.3.1 冒泡排序 【算法特点】 (1) 稳定排序。 (2) 可用于链式存储结构。 (3) 移动记录次数较多,算法平均时间性能比直接插入排序差。当初始记录无序,n较大时, 此算法不宜采用。 #include <stdio.h>#include <stdlib.h>#define MAXSIZE 26typedef int KeyType;typedef char In

【软考】希尔排序算法分析

目录 1. c代码2. 运行截图3. 运行解析 1. c代码 #include <stdio.h>#include <stdlib.h> void shellSort(int data[], int n){// 划分的数组,例如8个数则为[4, 2, 1]int *delta;int k;// i控制delta的轮次int i;// 临时变量,换值int temp;in

学习记录:js算法(二十八):删除排序链表中的重复元素、删除排序链表中的重复元素II

文章目录 删除排序链表中的重复元素我的思路解法一:循环解法二:递归 网上思路 删除排序链表中的重复元素 II我的思路网上思路 总结 删除排序链表中的重复元素 给定一个已排序的链表的头 head , 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表 。 图一 图二 示例 1:(图一)输入:head = [1,1,2]输出:[1,2]示例 2:(图