[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 ——每天在线,欢迎留言谈论. ...
随机推荐
- Linux系统下设置vi编辑器,tab键为4
1.cd ~ 2.vi .exrc 3.set tabstop=4(保存并退出)即可
- 树莓派2-SSH
树莓派3系统SSH是默认关闭的, 将SD卡插入linux, 在root 分区, 修改/etc/rc.local 在exit 0前增加一行 /ect/init.d/ssh start 将SD卡插回树莓派 ...
- stl sort和qsort的使用
好不容易使用了下stl的qsort函数,顺便和sort函数一起总结下: 很多时候我们都需要用到排序. 例如: 1 #include <iostream> #include <algo ...
- Python之字符串计算(计算器)
Python之字符串计算(计算器) import re expression = '-1-2*((60+2*(-3-40.0+42425/5)*(9-2*5/3+357/553/3*99/4*2998 ...
- 关于测试驱动的开发模式以及实战部分,建议看《Python Web开发测试驱动方法》这本书
关于测试驱动的开发模式以及实战部分,建议看<Python Web开发测试驱动方法>这本书
- Ubuntu中Hadoop环境搭建
Ubuntu中Hadoop环境搭建 JDK安装 方法一:通过命令行直接安装(不建议) 有两种java可以安装oracle-java8-installer以及openjdk (1)安装oracle-ja ...
- JavaEE JDBC 事务
JDBC 事务 @author ixenos 事务 1.概念:我们将一组语句构建成一个事务(trans action),当所有语句顺利执行之后,事务可以被提交(commit):否则,如果其中某个语句遇 ...
- MVC系统学习7—Action的选择过程
在Mvc源码的ControllerActionInvoker的InvokeAction方法里面有一个FindAction方法,FindAction方法在ControllerDescriptor里面定义 ...
- 2017 CCPC 杭州 HDU6265B 积性函数
题目链接 http://acm.hdu.edu.cn/downloads/CCPC2018-Hangzhou-ProblemSet.pdf B题 数论题 h(n)=∑ d|n φ(d) × ...
- Extjs6(六)——增删查改之查询
本文主要实现的效果是:点击查询按钮,根据form中的条件,在Grid中显示对应的数据(如果form为空,显示全部数据) 一.静态页面 1.查询按钮 { text:'查询', handler: 'onS ...