寒假12 图论

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstdio>
using namespace std;
int const N1 = 10010;
int const N2 = 100010;
int arr[N1];
int x[N2], y[N2];
int main()
{
	int n, m;
	cin >> n >> m;
	for (int i = 1;i <= m;i++)
	{
		scanf("%d%d", &x[i], &y[i]);
		arr[x[i]]++;
		arr[y[i]]++;
	}
	long long ans = 0;
	for (int i = 1;i <= m;i++)
	{
		ans += (2 * (arr[x[i]] - 1) * (arr[y[i]] - 1));
	}
	cout << ans << endl;
	return 0;
}
cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
int n, m;
using namespace std;
bool use[1010][1010];
int lu = 0;
bool arr[1010];
int cnt[1010];
void dfs(int num1, int num2)
{
	if (num1 == num2)
	{
		lu++;
		for (int i = 1;i <= n;i++)
		{
			if (arr[i]==1)cnt[i]++;
		}
	}
	else
	{
		for (int i = 1;i <= n;i++)
		{
			if (use[num1][i]==1&&arr[i]==0)
			{
				arr[i] = true;
				dfs(i, num2);
				arr[i] = false;
			}
		}
	}
}
int main()
{
	cin >> n >> m;
	int num1, num2;
	for (int i = 1;i <= m;i++)
	{
		scanf("%d%d", &num1, &num2);
		use[num1][num2] = true;
		use[num2][num1] = true;
	}
	cin >> num1 >> num2;
	dfs(num1, num2);
	int ans = 0;
	for (int i = 1;i <= n;i++)
	{
		if (cnt[i] == lu)ans++;
	}
	if (ans == 0)
	{
		cout << "-1" << endl;
	}
	else
	{
		cout << ans - 1 << endl;
	}
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
int n, m;
bool jj[1010][1010];
int dp[1010];
int main()
{
	cin >> n >> m;
	for (int i = 1;i <= n;i++)
	{
		dp[i] = i;
	}
	for (int i = 1;i <= m;i++)
	{
		int num1, num2;
		cin >> num1 >> num2;
		jj[num1][num2] = true;
	}
	for (int i = n;i >= 1;i--)
	{
		for (int j = n;j >=1 ;j--)
		{
			if (jj[j][i])
			{
				if (dp[j] < dp[i])
				{
					dp[j] = dp[i];
				}
			}
			else if (jj[i][j])
			{
				if (dp[i] < dp[j])
				{
					dp[i] = dp[j];
				}
			}
		}
	}
	for (int i = 1;i <= n;i++)
	{
		cout << dp[i] << " ";
	}
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
int n, m;
bool jj[1010][1010];
int dp[1010];
int dfs(int x)
{
	if (dp[x] != 0)return dp[x];
	int ans=x;
	for (int i = 1;i <= n;i++)
	{
		if (jj[x][i])
		{
			jj[x][i] = false;
			if (dfs(i) > ans)
			{
				ans = dfs(i);
			}
			jj[x][i] = true;
		}
	}
	dp[x] = ans;
	return ans;
}
int main()
{
	cin >> n >> m;
	/*for (int i = 1;i <= n;i++)
	{
		dp[i] = i;
	}*/
	for (int i = 1;i <= m;i++)
	{
		int num1, num2;
		cin >> num1 >> num2;
		jj[num1][num2] = true;
	}
	for (int i = 1;i <= n;i++)
	{
		cout<<dfs(i)<<" ";
	}
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
#include<queue>
int n;
int const N = 110;
bool vis[N][N];
queue<int>q;
int ans = 0;
bool use[N];
void bfs()
{
	while(ans!=n)
	{
		for (int i = 1;i <= n;i++)
		{
			if(use[i])
			{
				int flag = 0;
				for (int j = 1;j <= n;j++)
				{
					if (vis[j][i])
					{
						flag = 1;
					}
				}
				if (flag == 0)
				{
					ans++;
					use[i] = false;
					cout << i << " ";
					for (int j = 1;j <= n;j++)
					{
						vis[i][j] = false;
					}
				}
			}
		}
	}
}
int main()
{
	cin >> n;
	for (int i = 1;i <= n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			int num1;
			cin >> num1;
			if (num1 == 0)break;
			else
			{
				vis[i][num1] = true;
			}
		}
	}
	for (int i = 1;i <= n;i++)
	{
		use[i] = 1;
	}
	/*for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= n;j++)
		{
			if (vis[i][j])
			{
				cout << i << "-" << j << "  ";
			}
		}
		cout << endl;
	}*/
	bfs();
	return 0;

}
cpp 复制代码
#include<iostream>
using namespace std;
int const N = 210;
int n;
int arr[N][N];
int ans = 1000000;
void dfs(int u,int a)
{
	if (u == n)
	{
		if (a < ans)ans = a;
	}
	else
	{
		for (int j = u + 1;j <= n;j++)
		{
			dfs(j, (a + arr[u][j]));
		}
	}
}
int main()
{
	cin >> n;
	for (int i = 1;i < n;i++)
	{
		for (int j = i + 1;j <= n;j++)
		{
			cin >> arr[i][j];
		}
	}
	dfs(1,0);
	cout << ans << endl;
	return 0;
}
cpp 复制代码
#include<iostream>
#include<algorithm>
using namespace std;
int const N = 210;
int n;
int arr[N][N];
int dp[N];
int main()
{
	cin >> n;
	for (int i = 1;i < n;i++)
	{
		for (int j = i + 1;j <= n;j++)
		{
			cin >> arr[i][j];
		}
		dp[i] = 1000000000;
	}
	for (int i = n - 1;i >= 1;i--)
	{
		for (int j = i + 1;j <= n;j++)
		{
			dp[i] = min(dp[i] , arr[i][j] + dp[j]);
		}
	}
	cout << dp[1] << endl;
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
int const N = 1010;
bool vis[N][N];
int du[N];
int n, m;
int main()
{
	cin >> n >> m;
	for (int i = 1;i <= m;i++)
	{
		int u, v;
		cin >> u>>v;
		vis[u][v] = true;
		vis[v][u] = true;
		du[u]++;
		du[v]++;
	}
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= n;j++)
		{
			cout << vis[i][j] << " ";
		}
		cout << endl;
	}
	for (int i = 1;i <= n;i++)
	{
		cout << du[i] << " ";
		for (int j = 1;j <= n;j++)
		{
			if (vis[i][j])
			{
				cout << j << " ";
			}
		}
		cout << endl;
	}
	return 0;
}
cpp 复制代码
#include<iostream>
#include<cstring>
using namespace std;
int const N = 110;
int n;
bool g[N][N];
bool use[N];
void dfs(int u)
{
	for (int i = 1;i <= n;i++)
	{
		if (g[u][i] && use[i] == false)
		{
			use[i] = true;
			dfs(i);
		}
	}
}
int main()
{
	cin >> n;
	int ans = -1;
	for (int i = 1;i < n;i++)
	{
		int u, v;
		cin >> u >> v;
		g[v][u] = true;
	}
	int flag2 = 0;
	for (int i = 1;i <= n;i++)
	{
		flag2 = 0;
		memset(use, 0, sizeof(use));
		use[i] = true;
		dfs(i);
		for (int j = 1;j <= n;j++)
		{
			if (use[j] == false)
			{
				flag2 = 1;
			}
		}
		if (flag2 == 0)
		{
			ans = i;
			break;
		}
	}
	cout << ans << endl;
	return 0;
}
相关推荐
算法歌者12 分钟前
[算法]入门1.矩阵转置
算法
林开落L26 分钟前
前缀和算法习题篇(上)
c++·算法·leetcode
远望清一色27 分钟前
基于MATLAB边缘检测博文
开发语言·算法·matlab
tyler_download29 分钟前
手撸 chatgpt 大模型:简述 LLM 的架构,算法和训练流程
算法·chatgpt
Prejudices39 分钟前
C++如何调用Python脚本
开发语言·c++·python
单音GG42 分钟前
推荐一个基于协程的C++(lua)游戏服务器
服务器·c++·游戏·lua
SoraLuna1 小时前
「Mac玩转仓颉内测版7」入门篇7 - Cangjie控制结构(下)
算法·macos·动态规划·cangjie
我狠狠地刷刷刷刷刷1 小时前
中文分词模拟器
开发语言·python·算法
鸽鸽程序猿1 小时前
【算法】【优选算法】前缀和(上)
java·算法·前缀和
qing_0406031 小时前
C++——多态
开发语言·c++·多态