[Codeforces 623A] Graph and String
[题目链接]
http://codeforces.com/contest/623/problem/A
[算法]
首先 , 所有与其他节点都有连边的节点需标号为'b'
然后 , 我们任选一个节点 , 将其标号为'a' , 然后标记所以该节点能到达的节点
最后 , 我们需要检查这张图是否合法 , 只需枚举两个节点 , 若这两个节点均为'a'或'c' , 那么 , 若两个节点标号不同但有连边 , 不合法 , 如果两个节点标号相同但没有连边 , 也不合法
时间复杂度 : O(N ^ 2)
[代码]
#include<bits/stdc++.h>
using namespace std;
#define MAXN 510 struct edge
{
int to , nxt;
} e[MAXN * MAXN * ]; int tot , n , m;
int deg[MAXN],q[MAXN],head[MAXN];
char ans[MAXN];
bool finished[MAXN];
bool g[MAXN][MAXN]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u,int v)
{
tot++;
e[tot] = (edge){v,head[u]};
head[u] = tot;
} int main()
{ read(n); read(m);
for (int i = ; i <= m; i++)
{
int u , v;
read(u); read(v);
deg[u]++; deg[v]++;
addedge(u,v);
addedge(v,u);
g[u][v] = g[v][u] = true;
}
for (int i = ; i <= n; i++)
{
if (deg[i] == n - )
{
ans[i] = 'b';
finished[i] = true;
}
}
int l = , r = ;
for (int i = ; i <= n; i++)
{
if (!finished[i])
{
ans[i] = 'a';
finished[i] = true;
q[++r] = i;
break;
}
}
while (l <= r)
{
int cur = q[l++];
for (int i = head[cur]; i; i = e[i].nxt)
{
int v = e[i].to;
if (!finished[v])
{
finished[v] = true;
ans[v] = 'a';
q[++r] = v;
}
}
}
for (int i = ; i <= n; i++)
{
if (!finished[i])
ans[i] = 'c';
}
for (int i = ; i <= n; i++)
{
if (ans[i] == 'b') continue;
for (int j = ; j <= n; j++)
{
if (i == j || ans[j] == 'b') continue;
if (ans[i] == ans[j] && !g[i][j])
{
printf("No\n");
return ;
}
if (ans[i] != ans[j] && g[i][j])
{
printf("No\n");
return ;
}
}
}
printf("Yes\n");
for (int i = ; i <= n; i++) printf("%c",ans[i]);
printf("\n"); return ; }
[Codeforces 623A] Graph and String的更多相关文章
- codeforces 623A. Graph and String 构造
题目链接 给出一个图, 每个节点只有三种情况, a,b, c. a能和a, b连边, b能和a, b, c,连边, c能和b, c连边, 且无重边以及自环.给出初始的连边情况, 判断这个图是否满足条件 ...
- codeforces 624C Graph and String
C. Graph and String time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- AIM Tech Round (Div. 2) C. Graph and String 二分图染色
C. Graph and String 题目连接: http://codeforces.com/contest/624/problem/C Description One day student Va ...
- 图论:(Code Forces) Graph and String
Graph and String time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- AIM Tech Round (Div. 2) C. Graph and String
C. Graph and String time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 【CodeForces 624C】Graph and String
题 题意 n个表示abc三个字符的点,所有a和b是相连的,所有b和c是相连的,所有相同的是相连的,现在给你n个点和他们之间的m条边,判断是否存在这样的字符串,存在则给出一个符合条件的. 分析 我的做法 ...
- 【动态规划】【最短路】Codeforces 710E Generate a String
题目链接: http://codeforces.com/problemset/problem/710/E 题目大意: 问写N个字符的最小花费,写一个字符或者删除一个字符花费A,将当前的字符数量翻倍花费 ...
- codeforces 632C The Smallest String Concatenation
The Smallest String Concatenation 题目链接:http://codeforces.com/problemset/problem/632/C ——每天在线,欢迎留言谈论. ...
随机推荐
- 零基础入门学习Python(13)--元组:戴上了枷锁的列表
前言 这节课我们讨论主题是元祖:我们有个小标题戴上了枷锁的列表 我们都知道早在300多年前,孟德斯鸠在变法的时候说过,一切拥有权力的人都容易被滥用权力,这是万古不变的一条经验.但是呢,凡是拥有大权利的 ...
- CSS3 @font-face的url要添加?#iefix的原因
转至:https://github.com/CSSLint/csslint/wiki/Bulletproof-font-face When using @font-face to declare mu ...
- vue中的表单验证
http://www.cnblogs.com/luoxuemei/p/9295506.html /*是否合法IP地址*/ export function validateIP(rule, value, ...
- 84-Market Facilitation Index 市场促进指数指标.(2015.7.3)
Market Facilitation Index 市场促进指数指标 MFI指标的计算方式为: MFI=High(最高价)-Low(最低价))/ Volume(成交量) MFI上升,成交量上升,表示价 ...
- 19-看图理解数据结构与算法系列(Radix树)
Radix树 Radix树,即基数树,也称压缩前缀树,是一种提供key-value存储查找的数据结构.与Trie不同的是,它对Trie树进行了空间优化,只有一个子节点的中间节点将被压缩.同样的,Rad ...
- 一个IT工薪族的4年奋斗成果
关于标题:为了方便传播,使用了"最简化"的一段. 过段时间,考虑改为"大学毕业4年-回顾和总结(11):一个IT工薪族的4年奋斗成果(2012年6月17日~2016年6 ...
- Vue如何在data中正常引入图片路径
在Vue项目中通过data设置图片路径,然后在template中引入后页面无法显示图片,浏览器控制台报错: 刚开始以为是路径出问题了,于是绝对路径.相对路 ...
- 技能CD 效果 shader
技能CD特效 这个效果主要是利用反正切函数完成.atan2(x,y)的返回值是[-PI,PI],这个支持4个象限的反正切函数.关于圆角计算,在上篇文章中有介绍. 现在,我们来看看反正切函数的效果: 在 ...
- Codeforces Round #258 (Div. 2) D
D. Count Good Substrings time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- mysql pager用法&命令行命令
下面讲的命令,有部分只能在linux上才有.像pager命令windows上就没有了. 分屏:在Linux上,而且不是xwindow时,使用mysql命令行时,输出太多的东西,看不到就很悲剧了.在sh ...