麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

旅行商(TSP)->(拓?fù)渑判颍?/h1>
2019-11-10 19:15:36
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

旅行商(TSP)


Description

Shrek is a postman working in the mountain, whose routine work is sending mail to n villages. Unfortunately, road between villages is out of repair for long time, such that some road is one-way road. There are even some villages that can’t be reached from any other village. In such a case, we only hope as many villages can receive mails as possible.

Shrek hopes to choose a village A as starting point (He will be air-dropped to this location), then pass by as many villages as possible. Finally, Shrek will arrived at village B. In the travelling PRocess, each villages is only passed by once. You should help Shrek to design the travel route.

Input

There are 2 integers, n and m, in first line. Stand for number of village and number of road respectively.

In the following m line, m road is given by identity of villages on two terminals. From v1 to v2. The identity of village is in range [1, n].

Output

Output maximum number of villages Shrek can pass by.

Example

Input

4 31 42 44 3

Output

3

Restrictions

1 <= n <= 1,000,000

0 <= m <= 1,000,000

These is no loop road in the input.

Time: 2 sec

Memory: 256 MB

Hints

Topological sorting

描述

Shrek是一個(gè)大山里的郵遞員,每天負(fù)責(zé)給所在地區(qū)的n個(gè)村莊派發(fā)信件。但杯具的是,由于道路狹窄,年久失修,村莊間的道路都只能單向通過(guò),甚至有些村莊無(wú)法從任意一個(gè)村莊到達(dá)。這樣我們只能希望盡可能多的村莊可以收到投遞的信件。

Shrek希望知道如何選定一個(gè)村莊A作為起點(diǎn)(我們將他空投到該村莊),依次經(jīng)過(guò)盡可能多的村莊,路途中的每個(gè)村莊都經(jīng)過(guò)僅一次,最終到達(dá)終點(diǎn)村莊B,完成整個(gè)送信過(guò)程。這個(gè)任務(wù)交給你來(lái)完成。

輸入

第一行包括兩個(gè)整數(shù)n,m,分別表示村莊的個(gè)數(shù)以及可以通行的道路的數(shù)目。

以下共m行,每行用兩個(gè)整數(shù)v1和v2表示一條道路,兩個(gè)整數(shù)分別為道路連接的村莊號(hào),道路的方向?yàn)閺膙1至v2,n個(gè)村莊編號(hào)為[1, n]。

輸出

輸出一個(gè)數(shù)字,表示符合條件的最長(zhǎng)道路經(jīng)過(guò)的村莊數(shù)。

樣例

見(jiàn)英文題面

限制

1 ≤ n ≤ 1,000,000

0 ≤ m ≤ 1,000,000

輸入保證道路之間沒(méi)有形成環(huán)

時(shí)間:2 sec

空間:256 MB

提示

拓?fù)渑判?/p>

本來(lái)做這題是毫無(wú)思路的,后來(lái)看到了題下的提示說(shuō)要用拓?fù)渑判?/p>

然后思路就清晰了許多,寫(xiě)完debug了半天才想起來(lái)這OJ無(wú)法使用

C++容器,于是乖乖查了題解。

具體算法仍是拓?fù)渑判虻膫鹘y(tǒng)做法:

找到入度為0的點(diǎn)作為起點(diǎn),依次處理后繼,

唯一的不同是需要保存到達(dá)該城市的當(dāng)前最大值。

然后更新答案即可

#include<stdio.h>using namespace std;#define maxn 1000005int MAX(int a,int b){	if(a<b)		return b;	return a;}int n,m,cnt,q[maxn],degree[maxn];//q為拓?fù)鋽?shù)組---cnt為其長(zhǎng)度//degree為各點(diǎn)入度//n,m分別為點(diǎn)數(shù)和邊數(shù)int ans=1;//保存答案struct node{	int num;//村莊編號(hào)	node *next;	node()	{		next=NULL;	}	node(int x,node *n) :num(x),next(n){}};//該OJ無(wú)法使用C++容器,因此//只能現(xiàn)學(xué)自認(rèn)為重定義(看了題解)struct city{	node *nc;//next-city	int dp;//至此所通過(guò)的最大城市數(shù)	city(){nc=NULL;dp=1;}	void insert(int nc);}a[maxn];void city::insert(int nc){	degree[nc]++;	if(this->nc==NULL)		this->nc=new node(nc,NULL);	else	{		node *nodes=new node(nc,this->nc);		this->nc=nodes;	}	return;}void Topology(){	for(int i=1;i<=n;i++)		if(!degree[i])			q[++cnt]=i;	for(int i=1;q[i];i++)	{		int res=q[i];		for(node *tmp=a[res].nc;tmp!=NULL;tmp=tmp->next)		{			a[tmp->num].dp=MAX(a[res].dp+1,a[tmp->num].dp);			ans=MAX(ans,a[tmp->num].dp);			//處理后繼			int x=tmp->num;			degree[x]--;			if(!degree[x])				q[++cnt]=x;		}	}}int  main(){	scanf("%d%d",&n,&m);	for(int i=0;i<m;i++)	{		int x,y;		scanf("%d%d",&x,&y);		a[x].insert(y);	}	Topology();	printf("%d/n",ans);}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

主站蜘蛛池模板: 久久国产中文 | 欧美亚洲国产成人 | 国产精品高潮视频 | 一级免费观看 | 黄色一级片免费观看 | 欧美中文字幕一区二区 | 国产高潮国产高潮久久久91 | 19禁国产精品福利视频 | 最新亚洲视频 | 黄色一级片免费观看 | 99成人精品视频 | 日本在线不卡一区二区 | 日本看片一区二区三区高清 | av电影在线网 | 久久99精品久久 | 午夜久久电影 | 污视频在线免费播放 | 在线成人一区二区 | 看毛片电影| 污污短视频 | 97精品国产高清在线看入口 | 久国产精品视频 | 黄视频网站免费在线观看 | 激情亚洲一区二区三区 | 午夜爱爱福利 | 久久国产一二区 | 国产88久久久国产精品免费二区 | 中文字幕观看 | 精品国产一区三区| 精品无吗乱吗av国产爱色 | 欧美一级高清免费 | 国产欧美在线一区二区三区 | 午夜视频在线在免费 | 国产色片在线观看 | 欧美成人一区二区三区电影 | 二级大黄大片高清在线视频 | 成年人高清视频在线观看 | 精品国产一区二区三区在线观看 | 成人精品免费在线观看 | 成片免费大全 | 亚洲成人国产 |