寒假12 图论

寒假12 图论_第1张图片

#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
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;
}

寒假12 图论_第2张图片

#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
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;
}

 寒假12 图论_第3张图片

#include
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;
}
#include
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<

寒假12 图论_第4张图片

#include
using namespace std;
#include
int n;
int const N = 110;
bool vis[N][N];
queueq;
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;

}

 寒假12 图论_第5张图片

#include
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;
}
#include
#include
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;
}

寒假12 图论_第6张图片

#include
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;
}

 寒假12 图论_第7张图片

 

 

你可能感兴趣的:(算法,c++,数据结构)