5.24周六晚上-拓扑排序总结

· · 个人记录

拓扑排序

1.B3644 【模板】拓扑排序 / 家谱树

题目要求 整理关系,输出一个序列,使得每个人的后辈都比那个人后列出,即输出拓扑排序(图上点辈分关系的排序)。

思路

如果没有点指向某个点(入度为0),则那个点辈分最大,输出即可

①找到入度为0 的所有点,直接放入队列中

②将队列之中的点出队,然后将这个出对的点连出的边删除 ,相邻点的入度减一,如果 新的点入度为0,直接入队

时间复杂度:O(n)

注意

①拓扑排序不唯一

②只有有向无环图才存在拓扑序(DAG)

判断是否有拓扑序(有环)

if(cnt<n){//如果存在环,拓扑序一定没有n个数字 
    cout<<"有环";
}

Ac Code

#include<bits/stdc++.h>
using namespace std;
const int maxn=105;
vector<int> G[maxn];
int in[maxn];
int n; 
void topsort(){
    queue<int>q;
//  int cnt = 0;
    for(int i=1;i<=n;i++){
    //找辈分最大的点
        if(in[i]==0){
            q.push(i);//入队
        }
    }
    while(!q.empty()){
        int u=q.front();//出队
        q.pop();
        cout<<u<<' ';//输出topo序
//      cnt++
        for(int i=0;i<G[u].size();i++){
            in[G[u][i]]--;
            if(in[G[u][i]]==0){
        //新的点入度为0
                q.push(G[u][i]);//入队
            }
        }
    }
//  if(cnt<n){
//      cout<<"有换";
//  }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        int x;
        while(cin>>x&&x){
            G[i].push_back(x);//建边
            in[x]++;//统计入度
        }
    }
    topsort();
    return 0;
} 

2.D4282 【例4-13】奖金

拓扑+dp

先处理工资少的的,有先后关系,用拓扑排序

① 确定状态

dp[i]:表示i号员工的最少工资

② 确定答案

dp[1]到dp[n]之和

③ 确定状态转移方程

dp[v]=max(dp[u]+1,dp[v])(至少比之前的大1元)

④ 确定初始值和边界条件

dp[1]到dp[n]=100(最少工资100元)

Ac Code

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+10;
vector<int> G[maxn];
int in[maxn],dp[maxn];
int n,m; 
int ans=0;
void topsort(){
    int cnt=0;
    queue<int>q;
    for(int i=1;i<=n;i++){
        if(in[i]==0){
            q.push(i);
        }
    }
    while(!q.empty()){
        int u=q.front();
        q.pop();
        cnt++;
        for(int i=0;i<G[u].size();i++){
            int v=G[u][i];
            in[v]--;
            dp[v]=max(dp[u]+1,dp[v]);//至少比之前的大1元
            if(in[v]==0){
                q.push(v);
            }
        }
    }
    //有环则无合理方案
    if(cnt<n){
        cout<<"Poor Xed";
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    while(m--){
        int a,b;
        cin>>a>>b;
        G[b].push_back(a);
        in[a]++;
    }
    for(int i=1;i<=n;i++){
        dp[i]=100;
    }
    topsort();
    for(int i=1;i<=n;i++){
        ans+=dp[i];
    }
    cout<<ans;
    return 0;
} 

3.P1807 最长路

拓扑+dp

① 确定状态

dp[i]:示以1到i的最长路

② 确定答案

1.dp[n]=1e9 -1

2.dp[n]

③ 确定状态转移方程

dp[v]=max(dp[u]+w,dp[v]);

④ 确定初始值和边界条件

dp[1]=0;

dp[2到n]=-1e9;

Ac Code

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+510;
struct node{
    int v,w;//终点和边权
};
vector<node> G[maxn];
int in[maxn],dp[maxn];
int n,m; 
int ans=0;
void topsort(){
    dp[1]=0;
    queue<int>q;
    q.push(1);
    for(int i=2;i<=n;i++){
        if(in[i]==0){
            q.push(i);
        }
        dp[i]=-1e9;
    }
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for(int i=0;i<G[u].size();i++){
            int v=G[u][i].v;
            int w=G[u][i].w;
            in[v]--;
            dp[v]=max(dp[u]+w,dp[v]);
            if(in[v]==0){
                q.push(v);
            }
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    while(m--){
        int u,v,w;
        cin>>u>>v>>w;
        G[u].push_back({v,w});
        in[v]++;
    }
    topsort();
    if(dp[n]==-1e9){
        cout<<-1;
    }
    else{
        cout<<dp[n];
    }
    return 0;
} 

4.病毒

字符串字典序排序

1.从第一个开始比较字符,遇到小的则其字典序小
2.若全部相等,比较长度,长度长的,字典序大

无解情况

1. 能确定的字母种类少于目标单词的字母种类(条件不足)
2. 拓扑排序的结果不唯一
3. 拓扑排序有环
4.只有长度不相等,字典序从大到小,和题意矛盾

Ac Code

#include <bits/stdc++.h>
using namespace std;
string s[5005], t, std_s;
map<char, char> Map;
vector<char> G[125];
int in[125], tot;
bool vis[125];
char ans[27];
//获取图中的字符的字典序
string get_dict(){
    string tmp = "";
    for (char c = 'a'; c <= 'z'; c++){
        if (vis[c] == true){
            tmp += c;
       }
   }
    return tmp;
}
//判断不合法情况3
bool is_enough(){
    for (int i = 0; i < t.size(); i++){
        if (vis[t[i]] == false){
        return false;
        }
    }
    return true;
}
//可套用最长路的代码
bool topo_sort(){
    queue<char> q;
    int cnt = 0;
    for (char c = 'a'; c <= 'z'; c++){
        if (vis[c] == true && in[c] == 0){// 出现过的字母才能入队
            q.push(c);
            cnt++;
            //如果入度为0的点不唯一,拓扑排序结果不唯一,无解
            if (cnt > 1) {
                return false;
            }
       }
   }
    while (q.empty() == false){
        char cur = q.front();
        q.pop();
        ans[++tot] = cur;
        int cnt = 0;
        for (int i = 0; i < G[cur].size(); i++){
            char next = G[cur][i];
            in[next]--;
            if (in[next] == 0){
                cnt++; // 统计入度为0的点
                q.push(next);
           }
       }
        //同上
        if (cnt > 1){
            return false;
        }
   }
    std_s = get_dict();
    if (tot < std_s.size()){ // 有环
   return false;
 }
    return true;
}
void print(){
    for (int i = 1; i <= tot; i++){
        Map[ans[i]] = std_s[i - 1];
   }
    for (int i = 0; i < t.size(); i++){
        cout << Map[t[i]];
   }
    return;
}
int main(){
    int k;
    cin >> k;
    for (int i = 1; i <= k; i++){
        cin >> s[i];
        if (i == 1){
            continue;
        }
        for (int j = 0; j < min(s[i].size(), s[i - 1].size()); j++){
            char c1 = s[i - 1][j], c2 = s[i][j];
            if (c1 != c2){
                G[c1].push_back(c2);
                in[c2]++;
                vis[c1] = vis[c2] = true;
                break;//只找第一对不相同的字符
           }
       }
   }
    cin >> t;
    if (is_enough() == false || topo_sort() == false){
        cout << 0;
        return 0;
   }
    print();
    return 0;
}