CF #459 D. MADMAX
1 second
256 megabytes
standard input
standard output
As we all know, Max is the best video game player among her friends. Her friends were so jealous of hers, that they created an actual game just to prove that she's not the best at games. The game is played on a directed acyclic graph (a DAG) with n vertices and m edges. There's a character written on each edge, a lowercase English letter.
Max and Lucas are playing the game. Max goes first, then Lucas, then Max again and so on. Each player has a marble, initially located at some vertex. Each player in his/her turn should move his/her marble along some edge (a player can move the marble from vertex v to vertex u if there's an outgoing edge from v to u). If the player moves his/her marble from vertex v to vertex u, the "character" of that round is the character written on the edge from v to u. There's one additional rule; the ASCII code of character of round i should be greater than or equal to the ASCII code of character of round i - 1 (for i > 1). The rounds are numbered for both players together, i. e. Max goes in odd numbers, Lucas goes in even numbers. The player that can't make a move loses the game. The marbles may be at the same vertex at the same time.
Since the game could take a while and Lucas and Max have to focus on finding Dart, they don't have time to play. So they asked you, if they both play optimally, who wins the game?
You have to determine the winner of the game for all initial positions of the marbles.
The first line of input contains two integers n and m (2 ≤ n ≤ 100, ).
The next m lines contain the edges. Each line contains two integers v, u and a lowercase English letter c, meaning there's an edge from v to u written c on it (1 ≤ v, u ≤ n, v ≠ u). There's at most one edge between any pair of vertices. It is guaranteed that the graph is acyclic.
Print n lines, a string of length n in each one. The j-th character in i-th line should be 'A' if Max will win the game in case her marble is initially at vertex i and Lucas's marble is initially at vertex j, and 'B' otherwise.
- 4 4
1 2 b
1 3 a
2 4 c
3 4 b
- BAAA
ABAA
BBBA
BBBB
- 5 8
5 3 h
1 2 c
3 1 c
3 2 r
5 1 r
4 3 z
5 4 r
5 2 h
- BABBB
BBBBB
AABBB
AAABA
AAAAB
题意:给你一个有向无环图,两个人玩游戏,规则是A先走然后B走,附加条件是走的第i步的那条边的权值要大于等于上一步边的权值,然后输出A,B所有起点的共n*n种情况的胜负。
我的做法是从末端考虑,相当于进行拓扑,把所有出度为0的点放入到一个own已有队列中,每当有一个新的出度为0的点进入时,就把这个点和其它所有点所组成的情况进行dp,就是枚举这个点的
下一步情况,然后再枚举另一个点的下一步情况,相当于两个for循环,同时为应对附加条件要对走的起点边进行记录,所以开三维dp,因为是倒着向前推得,所以记录的是起点边,更新的时候就
把小于等于起点边的同时也更新掉,第三位的意思是小于等于即可利用这个。。。- 然后别人的做法是直接从头往后走就ok了,然后记忆化记忆一下,比我的优美多了。。。
- #include<cstdio>
- #include<vector>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- const int N=;
- int dp[N][N][],n,ru[N],m,q[N],own[N];
- vector<int>F[N];
- vector<pair<int,int> >Z[N];
- int main(){
- scanf("%d%d",&n,&m);
- int x,y,l=,r=,tot=;char z;
- for(int i=;i<=m;++i) {
- scanf("%d %d %c",&x,&y,&z);
- Z[x].push_back(make_pair(y,(int)z));
- F[y].push_back(x);
- ++ru[x];
- }
- for(int i=;i<=n;++i) if(!ru[i]) q[r++]=i;
- while(l<r) {
- int u=q[l++];
- for(int i=;i<tot;++i) for(int j=;j<(int)Z[u].size();++j)
- {
- bool ok=;
- for(int k=;k<(int)Z[own[i]].size();++k) if(Z[own[i]][k].second>=Z[u][j].second) ok&=dp[Z[u][j].first][Z[own[i]][k].first][Z[own[i]][k].second];
- dp[u][own[i]][Z[u][j].second]|=ok;
- if(ok) for(int l=(int)'a';l<=Z[u][j].second;++l) dp[u][own[i]][l]=;
- }
- for(int i=;i<tot;++i) for(int j=;j<(int)Z[own[i]].size();++j)
- {
- bool ok=;
- for(int k=;k<(int)Z[u].size();++k) if(Z[own[i]][j].second<=Z[u][k].second) ok&=dp[Z[own[i]][j].first][Z[u][k].first][Z[u][k].second];
- dp[own[i]][u][Z[own[i]][j].second]|=ok;
- if(ok) for(int l=(int)'a';l<=Z[own[i]][j].second;++l) dp[own[i]][u][l]=;
- }
- for(int i=;i<(int)F[u].size();++i) {
- int v=F[u][i];
- --ru[v];
- if(ru[v]==) q[r++]=v;
- }
- own[tot++]=u;
- }
- for(int i=;i<=n;++i) {
- for(int j=;j<=n;++j) if(dp[i][j][(int)'a']) putchar('A');else putchar('B');
- puts("");
- }
- }
http://blog.csdn.net/winter2121/article/details/79203764//别人的解法
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- int n,m,x,y,dp[][][],dis[][];
- int DP(int s,int t,int w){
- if(dp[s][t][w-'a']>=) return dp[s][t][w-'a'];
- for(int i=;i<=n;++i) if(dis[s][i]>=w&&(DP(t,i,dis[s][i])==)) return dp[s][t][w-'a']=;
- return dp[s][t][w-'a']=;
- }
- int main(){
- char z;
- scanf("%d%d",&n,&m);
- for(int i=;i<=m;++i) {
- scanf("%d %d %c",&x,&y,&z);
- dis[x][y]=z;
- }
- memset(dp,-,sizeof(dp));
- for(int i=;i<=n;++i) for(int j=;j<=n;++j) DP(i,j,'a');
- for(int i=;i<=n;++i) {
- for(int j=;j<=n;++j) printf("%c",dp[i][j][]?'A':'B');
- puts("");
- }
- }
Here's the graph in the first sample test case:
Here's the graph in the second sample test case:
CF #459 D. MADMAX的更多相关文章
- Codeforces Round #459 (Div. 2) D. MADMAX DFS+博弈
D. MADMAX time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)
D. MADMAX time limit per test1 second memory limit per test256 megabytes Problem Description As we a ...
- CF Round #459
好菜啊 第一场cf就菜成这样...mdzz 可能是我把题看的太简单了吧... T1AC T2AC T3WA T4看错题 T5不会写 T3想的是栈+暴力 正解: 对于一个pretty串的任意一个位置, ...
- Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)
题意 在一个有向无环图上,两个人分别从一个点出发,两人轮流从当前点沿着某条边移动,要求经过的边权不小于上一轮对方经过的边权(ASCII码),如果一方不能移动,则判负.两人都采取最优策略,求两人分别从每 ...
- 【Codeforces Round #459 (Div. 2) D】MADMAX
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] f[x][y][z][2] 表示第一个人到了点x,第二个人到了点y,当前轮的字母(1..26),当前轮到谁走的情况下,谁赢. 写个记 ...
- Codeforces Round #459 (Div. 2)
A. Eleven time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- codeforces 459 A. Pashmak and Garden 解题报告
题目链接:http://codeforces.com/problemset/problem/459/A 题目意思:给出两个点的坐标你,问能否判断是一个正方形,能则输出剩下两点的坐标,不能就输出 -1. ...
- ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'
凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
随机推荐
- C++操作Kafka使用Protobuf进行跨语言数据交互
C++操作Kafka使用Protobuf进行跨语言数据交互 Kafka 是一种分布式的,基于发布 / 订阅的消息系统.主要设计目标如下: 以时间复杂度为 O(1) 的方式提供消息持久化能力,即使对 T ...
- tomcat启动日志中中文乱码
场景 使用catalina.bat start命令启动tomcat7 方案 打开%catalina_home%/conf/logging.properties文件. 将其中的UTF-8代换为GBK. ...
- js的同步与异步
JavaScript语言的一大特点就是单线程,也就是说,同一个时间只能做一件事.那么,为什么JavaScript不能有多个线程呢?这样能提高效率啊. JavaScript的单线程,与它的用途有关.作为 ...
- FastJson踩坑:@JsonField在反序列化时失效
问题描述 一个对象(某个字段为枚举类型,为了不采用默认的序列化过程,用@JSONField指定了序列化器和反序列器,过程见旧博文),将其放到JSONArray中再序列化JSONArray对象,用得到的 ...
- 团队一致性的PHP开发环境之Docker
docker php环境模型 docker 简介 Docker 是一个开源的应用容器引擎 让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现 ...
- 以内存级速度实现存储?XPoint正是我们的计划
随着计算能力虚拟化技术的普及,存储机制在速度上远逊于内存这一劣势开始变得愈发凸显. 这一巨大的访问速度鸿沟一直是各项存储技术想要解决的核心难题:纸带.磁带.磁盘驱动器乃至闪存记忆体等等,而如今最新一代 ...
- 谈谈JavaScript中的变量、指针和引用
1.变量 我们可能产生这样一个疑问:编程语言中的变量到底是什么意思呢? 事实上,当我们定义了一个变量a时,就是在存储器中指定了一组存储单元,并将这组存储单元命名为a.变量a的值实际上描述的是这组存储单 ...
- windows 7或以上系统的实用小工具,你知道么?
今晚给大家介绍个实用的好工具,可以做简单的问题记录,再也不用截图加注释这么辛苦了····· 经测试,这东东在win7,2008 及2008R2里适用,也就是说,在win7以上的系统中才有.好了,下面直 ...
- docker批量删除本地镜像和容器
长时间运行docker,每次只用docker kill去停止容器,但是从没删除过本地镜像,导致有上百个镜像在占用内存. 1.批量停止容器 docker container stop $(docker ...
- 数据可视化:使用python代码实现可视数据随机漫步图
#2020/4/5 ,是开博的第一天,希望和大家相互交流学习,很开森,哈哈~ #像个傻子哟~ #好,我们进入正题, #实现功能:利用python实现数据随机漫步,漫步点数据可视化 #什么是 ...