链式前向星复习图论

B3862 图的遍历(简单版) - 洛谷 | 计算机科学教育新生态

复制代码
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1e5+10;
int head[N];
int tot;
int n,m;
int pos;
bool vis[N];
struct Edge{
	int next;
	int to;
}e[N];
void init()
{
	for(int i = 1 ; i <= n ; i++)
	head[i] = -1;
}
void add(int u,int v)
{
	++tot;
	e[tot].next = head[u];
	e[tot].to = v;
	head[u] = tot;
}
void dfs(int current)
{
	if(current > pos)
	pos = current;
	int k = head[current];//取出连接第一条边的信息 

	while(k != -1)
	{
		int to = e[k].to;//指向的点
		int temp = head[to];//链表to点连接第一条边 
		if(vis[to] == false)
		{	
			vis[to] = true;
			dfs(to);
		}
		k = e[k].next;
	}
}
int main(void)
{
	cin >> n >> m;
	init();
	for(int i = 1 ; i <= m ; i++)
	{
		int u,v;
		cin >> u >> v;
		add(u,v);
	}
	for(int i = 1 ; i <= n ; i++)
	{
		memset(vis,false,sizeof(vis));
		vis[i] = true;
		pos = i;
		dfs(i);
		cout << pos << " "; 
	}
	return 0;
}

P3916 图的遍历 - 洛谷 | 计算机科学教育新生态

90分

复制代码
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1e5+10;
int head[N];
int tot;
int n,m;
int pos;
bool vis[N];
struct Edge{
	int next;
	int to;
}e[N];
void init()
{
	for(int i = 1 ; i <= n ; i++)
	head[i] = -1;
}
void add(int u,int v)
{
	++tot;
	e[tot].next = head[u];
	e[tot].to = v;
	head[u] = tot;
}
void dfs(int current)
{
	if(current > pos)
	pos = current;
	int k = head[current];//取出连接第一条边的信息 

	while(k != -1)
	{
		int to = e[k].to;//指向的点
		int temp = head[to];//链表to点连接第一条边 
		if(vis[to] == false)
		{	
			vis[to] = true;
			dfs(to);
		}
		k = e[k].next;
	}
}
int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	init();
	for(int i = 1 ; i <= m ; i++)
	{
		int u,v;
		cin >> u >> v;
		add(u,v);
	}
	for(int i = 1 ; i <= n ; i++)
	{
		memset(vis,false,sizeof(vis));
		vis[i] = true;
		pos = i;
		dfs(i);
		cout << pos << " "; 
	}
	return 0;
}

100分

复制代码
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1e5+10;
int head[N];
int tot;
int n,m;
int pos;
bool vis[N];
int ans[N];
struct Edge{
	int next;
	int to;
}e[N];
void init()
{
	for(int i = 1 ; i <= n ; i++)
	head[i] = -1;
}
void add(int u,int v)
{
	++tot;
	e[tot].next = head[u];
	e[tot].to = v;
	head[u] = tot;
}
void dfs(int current,int maxpos)
{
	if(vis[current] == false)
	ans[current] = maxpos;
	vis[current] = true;
	int k = head[current];
	while(k != -1)
	{
		int to = e[k].to;//指向的点
		int temp = head[to];//链表to点连接第一条边 
		if(vis[to] == false)
		{	
			ans[to] = maxpos;
			vis[to] = true;
			dfs(to,maxpos);
		}
		k = e[k].next;
	}
}
int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	init();
	for(int i = 1 ; i <= m ; i++)
	{
		int u,v;
		cin >> u >> v;
		add(v,u);//方向建立边 
	}
	for(int i = n ; i >= 1 ; i--)
	{
		dfs(i,i); 
	}
	for(int i = 1 ; i <= n ; i++)
	cout << ans[i] << " ";
	return 0;
}

P1700 [USACO19OPEN] Milk Factory B - 洛谷 | 计算机科学教育新生态

复制代码
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1e5+10;
int head[N];
int ans = 1e9;
int cnt;
int tot;
int n;
bool vis[N];
struct Edge{
	int next;
	int to;
}e[N];
bool found;
void add(int u,int v)
{
	tot++;
	e[tot].next = head[u];
	e[tot].to = v;
	head[u] = tot;
}
void dfs(int current)
{
	vis[current] = true;
	cnt++;
	int k = head[current];
	while(k != -1)
	{
		int to = e[k].to;
		if(vis[to] == false)
		{
			vis[to] = true;
			dfs(to);
		}
		k = e[k].next;
	}
}
int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	
	for(int i = 1 ; i <= n ; i++)
	head[i] = -1;
	
	for(int i = 1 ; i <= n-1 ; i++)
	{
		int u,v;
		cin >> u >> v;
		add(v,u);//反向建立边
	}
	for(int i = 1 ; i <= n ; i++)
	{
		cnt = 0;
		memset(vis,false,sizeof(vis));
		dfs(i);
		if(cnt == n)
		{
			found = true;
			ans = min(ans,i);
		}
	}
	if(found)
	cout << ans;
	else
	cout << -1;
	return 0;
}
相关推荐
被AI抢饭碗的人几秒前
算法题(240):最大食物链计数
算法
熬了夜的程序员3 分钟前
【LeetCode】82. 删除排序链表中的重复元素 II
数据结构·算法·leetcode·链表·职场和发展·矩阵·深度优先
欧克小奥18 分钟前
Floyd判圈算法(Floyd Cycle Detection Algorithm)
算法·floyd
熬了夜的程序员1 小时前
【LeetCode】83. 删除排序链表中的重复元素
算法·leetcode·链表
胖咕噜的稞达鸭2 小时前
AVL树手撕,超详细图文详解
c语言·开发语言·数据结构·c++·算法·visual studio
YSRM2 小时前
Leetcode+Java+图论II
java·leetcode·图论
熊猫钓鱼>_>2 小时前
Rust语言特性深度解析:所有权、生命周期与模式匹配之我见
算法·rust·软件开发·函数·模式匹配·异步编程·质量工具
芒果量化2 小时前
Optuna - 自动调参利器&python实例
开发语言·python·算法·机器学习
麦麦大数据2 小时前
D025 摩托车推荐价格预测可视化系统|推荐算法|机器学习|预测算法|用户画像与数据分析
mysql·算法·机器学习·django·vue·推荐算法·价格预测
CoovallyAIHub3 小时前
IDEA研究院发布Rex-Omni:3B参数MLLM重塑目标检测,零样本性能超越DINO
深度学习·算法·计算机视觉