1. 程式人生 > >codeforces 917B MADMAX 拓撲排序

codeforces 917B MADMAX 拓撲排序

傳送門
題目大意:給你一張有向無環圖,邊權是字母。
對於每一對(s,t),按照如下規則,求當第一個人在s第二個人在t的時候誰有必勝策略。規則是,兩人輪流移動,每次移動的邊權不能小於上一次移動的邊權。不能移動的人輸。n<=100, m<=10000。
題解:DAG,考慮dp。兩個人博弈論,考慮令f[x][y][c]表示第一個人在x第二個人在y上一次邊權是c,當前是第一個人要走,能否贏。g同理,表示第二個人。按照拓撲序轉移即可。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define N 110 #define M 10010 using namespace std; bool f[N][N][30],g[N][N][30]; struct edges{ int to,pre,wgt; }e[M];int h[N],etop,cnt,d[N],t[N]; queue<int> q;char ch[5]; inline int add_edge(int u,int v,int w) { return e[++etop].to=v,e[etop].wgt=w,e[etop].pre=h[u],h[u]=etop; } int main() { int
n,m;scanf("%d%d",&n,&m); for(int i=1,u,v;i<=m;i++) scanf("%d%d%s",&u,&v,ch), add_edge(u,v,ch[0]-'a'+1),d[v]++; for(int i=1;i<=n;i++) if(!d[i]) q.push(i),t[++cnt]=i; while(!q.empty()) { int x=q.front();q.pop(); for(int i=h[x],y;i;i=e[i].pre) if
(!(--d[y=e[i].to])) q.push(y),t[++cnt]=y; } for(int i=n;i;i--) for(int j=n;j;j--) for(int c=0;c<=26;c++) { int x=t[i],y=t[j];f[x][y][c]=g[x][y][c]=false; for(int k=h[x];k;k=e[k].pre) if(c<=e[k].wgt) f[x][y][c]|=(!g[e[k].to][y][e[k].wgt]); for(int k=h[y];k;k=e[k].pre) if(c<=e[k].wgt) g[x][y][c]|=(!f[x][e[k].to][e[k].wgt]); } for(int i=1;i<=n;i++,printf("\n")) for(int j=1;j<=n;j++) if(f[i][j][0]) printf("A"); else printf("B"); return 0; }