跳到正文

个人记录

7-18课程解析+总结

xiangrui0305约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月12日
篇幅
约 11 分钟

版本

  1. v1当前

7-18课程解析+总结

题目解析+总结

U460109

给出NN个点,MM条边的无向图,要求输出这个图的连通块个数。(注意,可能会有重边和自环)

分析

一:图形结构存储:将输入好的数据存入数组中。注意考虑重边和自环。

C++
cin>>x>>y;
if(x == y || find(op[x].begin(),op[x].end(),y) != op[x].end()) continue;
op[x].push_back(y);
op[y].push_back(x);

二:BFS找联通块:遍历一下,将连通块里的所有数字全部标记,接着加上连通块的数量cnt。

C++
op2.push(cur);
while(op2.size()){
    int top = op2.front();
    op2.pop();
    for(int i=0;i<op[top].size();++i){
        int v = op[top][i];
        if(vis[v]) continue;
        vis[v] = 1;
        op2.push(v);
    }
}
代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,cnt = 0;
vector<int>op[200005];
map<int,bool>vis;
queue<int>op2;
void BFS(int cur){
	op2.push(cur);
	while(op2.size()){
		int top = op2.front();
		op2.pop();
		for(int i=0;i<op[top].size();++i){
			int v = op[top][i];
			if(vis[v]) continue;
			vis[v] = 1;
			op2.push(v);
		}
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=m;++i){
		int x,y;
		cin>>x>>y;
		if(x == y || find(op[x].begin(),op[x].end(),y) != op[x].end()) continue;
		op[x].push_back(y);
		op[y].push_back(x);
	}
	for(int i=1;i<=n;++i){
		if(!vis[i]){
			cnt++;
			vis[i] = 1;
			BFS(i);
		}
	}
	cout<<cnt<<endl;
	return 0;
}
总结

图形结构存储 + BFS找联通块

P1294

给出NN个点,MM条边的无向图,每条边长度为XX。定义第xx个点为起点,从起点开始,每个点最多经过一次。请问能走多长的路?

分析

一:图形结构存储:将输入好的数据存入数组中。无需考虑重边和自环。

C++
for(int i=1;i<=m;++i){
    int x,y,w1;
    cin>>x>>y>>w1;
    op[x].push_back({y,w1});
    op[y].push_back({x,w1});
}

二:DFS找最长路:遍历的时候将函数第二个参数——sum更新(存入maxx中)。并且记得回溯!!!

C++
maxx = max(maxx,sum);
for(int i=0;i<op[cur].size();++i){
    int v1 = op[cur][i].v, w1 = op[cur][i].w;
    if(vis[v1]) continue;
    vis[v1] = 1;
    DFS(v1,sum+w1);
    vis[v1] = 0;
}
代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,maxx = 0;
struct node{
	int v,w;
};
vector<node>op[21];
bool vis[1005];
void DFS(int cur,int sum){
	maxx = max(maxx,sum);
	for(int i=0;i<op[cur].size();++i){
		int v1 = op[cur][i].v, w1 = op[cur][i].w;
		if(vis[v1]) continue;
		vis[v1] = 1;
		DFS(v1,sum+w1);
		vis[v1] = 0;
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=m;++i){
		int x,y,w1;
		cin>>x>>y>>w1;
		op[x].push_back({y,w1});
		op[y].push_back({x,w1});
	}
	for(int i=1;i<=n;++i){
		memset(vis,0,sizeof vis);
		vis[i] = 1;
		DFS(i, 0);
	}
	cout<<maxx<<endl;
	return 0;
}
总结

图形结构存储 + DFS找最长路

P1700

给出NN个点,MM条边的有向图,如果这个图无论怎么定义起点,终点永远是第ii个点,就请求出这个点是什么。如果找不到这个点,请输出−1-1。

分析

一:图形结构存储:将输入好的数据存入数组中。注意是有向图。

C++
cin>>x>>y;
op[x].push_back(y);

二:DFS走迷宫:遍历一下,同时要是发现有条路没走过就标记flag。若flag一直没被标记过,说明来到了终点,就加上终点的位置。注意更新vis。

C++
bool flag = 0;
  	for(int i=0;i<op[cur].size();++i){
    int v = op[cur][i];
    if(vis[v]) continue;
    flag = 1;
    vis[v] = 1;
    DFS(v);
}
if(!flag) mp[cur]++;

三:map迭代器:要是某一个终点的数量等于图的点数,则说明这是每个店的唯一终点,则输出终点的具体位置;否则输出−1-1。

C++
for(auto x:mp){
    if(x.second == n){
        cout<<x.first<<endl;
        return 0;
    }
}
cout<<-1<<endl;
代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
vector<int>op[105];
map<int,int>mp;
bool vis[105];
void DFS(int cur){
	bool flag = 0;
	for(int i=0;i<op[cur].size();++i){
		int v = op[cur][i];
		if(vis[v]) continue;
		flag = 1;
		vis[v] = 1;
		DFS(v);
	}
	if(!flag) mp[cur]++;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>n;
	for(int i=1;i<n;++i){
		int x,y;
		cin>>x>>y;
		op[x].push_back(y);
	}
	for(int i=1;i<=n;++i){
		memset(vis,0,sizeof vis);
		vis[i] = 1;
		DFS(i);
	}
	for(auto x:mp){
		if(x.second == n){
			cout<<x.first<<endl;
			return 0;
		}
	}
	cout<<-1<<endl;
	return 0;
}
总结

图形结构存储 + DFS走迷宫 + map迭代器

homework~

CF94B(codefroces都不能交)

代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int m;
vector<int>op[6];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin>>m;
    for(int i=1;i<=m;++i){
    	int x,y;
    	cin>>x>>y;
    	op[x].push_back(y);
    	op[y].push_back(x);
	}
	for(int i=1;i<=5;++i){
		for(int j=1;j<=5;++j){
			for(int k=1;k<=5;++k){
				bool a = op[i][j] && op[j][k] && op[i][k];
                bool b = !op[i][j] && !op[j][k] && !op[i][k];
                if(a || b){
                	cout<<"WIN\n";
                	return 0;
				}
			}
		}
	}
	cout<<"FAIL\n";
	return 0;
}

U460106

代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
int tree[5005];
int picture[5005];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<n;++i){
    	int x,y;
    	cin>>x>>y;
    	tree[min(x,y)]++;
	}
	for(int i=1;i<=m;++i){
    	int x,y;
    	cin>>x>>y;
    	picture[min(x,y)]++;
	}
	int cnt = 0;
	for(int i=1;i<=n;++i){
		cnt += abs(tree[i]-picture[i]);
	}
	cout<<cnt<<endl;
	return 0;
}

P10625

代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
int ru[1001],chu[1001];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;++i){
    	for(int j=1;j<=n;++j){
    		bool a;
    		cin>>a;
    		if(a == 1){
    			ru[i]++;
    			chu[j]++;
			}
		}
	}
	cout<<ru[m]<<' '<<chu[m]<<' '<<ru[m]+chu[m];
	return 0;
}
原文