1.病毒溯源
问题:求树的最长链长度和字典序最小的最长链
思路:
一开始用 bfs 做的 , 一直是段错误 , 很迷惑 , 最后换成了 dfs , 深搜比广搜使用的空间少。
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
typedef long long ll;
const int N = 1e4+10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
const int inf = 1 << 31 - 1;
const double eps = 1e-9;
int n , c , k , root , mx;
vector<int>ve[N];
bool vis[N];
vector<int>ans , now;
void dfs(int id,int h){
if(h > mx){ mx = h; ans = now; }
for(auto k : ve[id]){
now.push_back(k);
dfs(k , h + 1);
now.pop_back();
}
}
int main(){
cin >> n;
for(int i=0;i<n;i++){
cin >> c;
for(int j=1;j<=c;j++){
cin >> k;vis[k] = 1;
ve[i].push_back(k);
}
sort(ve[i].begin(),ve[i].end());
//排序 , 保证最先遇到的最长串一定是字典序最小的;
}
for(int i=0;i<n;i++) if(!vis[i]) now.push_back(i) , dfs(i,1);
cout << mx << "\n";
// for(auto k : ans) cout << k << " ";
for(int i=0;i<(int)ans.size();i++){
if(i != (int)ans.size() - 1)
cout << ans[i] << " ";
else
cout << ans[i];
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
2.龙龙送外卖
思路:
送餐完成后不返回起点的最小距离 = 总距离 - 最深节点深度
所以原问题分解成了两个问题:
第一个问题是要求总距离 , 也就是到达所有节点需要的边数 × 2
第二个问题就是要求 : 最深节点的深度
如果我们每加入一个节点都从根节点开始搜索 , 搜索上面的两个值 , 不仅费力 , 而且会TLE ,这样就需要我们用另一种思路 , 那就是“反着搜” , 当加入某个节点时 , 从当前节点往根节点搜 , 我们结合记忆化搜索 , 就很轻松地可以求出当前节点的深度 , 而到达所有节点需要的边数也就变成了前面所有点到达根节点经过的不同点的数量
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
typedef long long ll;
const int N = 1e5+10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
const int inf = 1 << 31 - 1;
const double eps = 1e-9;
int n , m , u , mx_h , all;
int fa[N] , h[N];
int dfs(int x){
if(fa[x] == -1 || h[x]) return h[x];//记忆化搜索记录深度
all++;//记录不同的节点数
return h[x] = dfs(fa[x]) + 1;
}
int main(){
IOS
cin >> n >> m;
for(int i=1;i<=n;i++) cin >> fa[i];
for(int i=1;i<=m;i++){
cin >> u;
mx_h = max(mx_h , dfs(u));
cout << all * 2 - mx_h << "\n";
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
3.红色警报:
思路:
根据题意的描述 , 不难看出描述出来的是一个并查集的逆过程:从联通集里拆点 , 问拆到那个点会使联通集变多(发出红色警报);
正着想肯定是没什么思路 , 不如我们反着想 , 就是找加入哪些点会使联通集变少 , 这就正好是并查集的过程 , 所以题解就是按照拆点的逆序去做合并 , 找到我们的目标点;
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
typedef long long ll;
const int N = 5e2+10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
const int inf = 1 << 31 - 1;
const double eps = 1e-9;
int n , m , k , u;
vector<int>ve[N];
int fa[N] , a[N] , dp[N];
bool vis[N];
map<int,int>mp;
int find(int x){
if(fa[x] == x) return x;
else return fa[x] = find(fa[x]);
}
void unionn(int x,int y){
int xx = find(x);
int yy = find(y);
fa[xx] = yy;
}
int main(){
cin >> n >> m;
for(int i=0;i<n;i++) fa[i] = i;
for(int i=1;i<=m;i++){
int u , v;
cin >> u >> v;
ve[u].push_back(v);
ve[v].push_back(u);
}
cin >> k;
for(int i=1;i<=k;i++) cin >> a[i] , mp[a[i]] = 1;
int cnt = k;
for(int i=0;i<n;i++){
if(!mp[i]) a[++cnt] = i;
}//注意不要漏掉 k 个点之外的点
for(int i=cnt;i>=1;i--){
u = a[i];
for(auto s : ve[u]) if(vis[s]) unionn(u,s);
vis[u] = 1;
for(int j=0;j<n;j++) if(j == find(j) && vis[j]) dp[i]++;
}
for(int i=1;i<=k;i++){
//拆点之后联通集变多
if(dp[i] < dp[i+1]){
cout << "Red Alert: ";
printf("City %d is lost!\n",a[i]);
}else{
printf("City %d is lost.\n",a[i]);
}
//注意处理 全部城市都毁灭
if(i == n) cout << "Game Over.";
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);