[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 ——每天在线,欢迎留言谈论. ...
随机推荐
- tomcat:页面跳转
vim index.html <script language="javascript"type="text/javascript"> window ...
- python 开发搭建目录规范
软件目录结构规范有什么好处: 通过规范化,能够更好的控制软件结构,让程序具有更高的可读性. 项目目录组织结构如下: Foo/ # 项目名 --bin/ # 可执行文件目录 --foo # 可执行程序 ...
- table中JS选取行列
<table border="1" align="center" style="width: 500px" id="doct ...
- Poj 1106 Transmitters
Poj 1106 Transmitters 传送门 给出一个半圆,可以任意旋转,问这个半圆能够覆盖的最多点数. 我们枚举每一个点作为必然覆盖点,那么使用叉积看极角关系即可判断其余的点是否能够与其存在一 ...
- Xcode报referenced from错误的总结
一.库文件丢失 如果提示的文件是库文件,比如说是sdk的文件,有可能是就是丢失,或者没有引用到该工程. 1.点击这个.a库,或者framework,看右边的target里面是否引用到了当前的targe ...
- 【51NOD1766】树上的最远点对(线段树,LCA,RMQ)
题意:n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间, 表示点的标号请你求出两个区间内各选一点之间的最大距离,即你需要求出max{dis(i,j) |a<=i<=b,c< ...
- POJ 3684_Physics Experiment
题意: 若干球最初从高到低排列,依次落下. 球与地面碰撞,速度不变方向相反,球之间碰撞, 交换速度和方向.问某一时刻各个球的高度. 分析: 把球之间的碰撞看成是擦肩而过,但是由于半径的存在,最后每个球 ...
- [bzoj5379]Tree_dfs序_线段树_倍增lca
Tree bzoj-5379 题目大意:给定一棵$n$节点的树.支持:换根.把节点$u$和$v$的$lca$的子树加.询问$u$的子树和. 注释:$1\le n,q\le 3\times 10^5$. ...
- JSP的客户端请求
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/client-request.html: 当浏览器请求一个网页时,它向Web服务器发送大量的信息,信息不能 ...
- sql 按中文排序
sql server:select * from [表名]order by [字段],[字段] collate Chinese_PRC_CS_AS_KS_WS mysql:select * from ...