hihoCoder #1322 : 树结构判定
传送门:https://hihocoder.com/problemset/problem/1322
题意:给出一个n个顶点m条边的无向图,问是不是树。
思路:首先作为一棵树必然满足n=m+1,若满足则任取一点dfs判断有没有圈,最后看是不是所有点都遍历到了。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<utility>
#include<algorithm>
#include<utility>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<map>
#include<ctime>
#include<functional>
#include<bitset>
#define P pair<int,int>
#define ll long long
#define ull unsigned long long
#define lson id*2,l,mid
#define rson id*2+1,mid+1,r
#define ls id*2
#define rs (id*2+1)
#define Mod(a,b) a<b?a:a%b+b
#define cl0(a) memset(a,0,sizeof(a))
#define cl1(a) memset(a,-1,sizeof(a))
using namespace std;
const ll M = 1e9 + 7;
const ll INF = 1e9;
const int N = 410;
const double _e = 10e-6;
const int maxn = 2e5;
const int matSize = 9;
const int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };
const int _dx[8] = { -1,-1,-1,0,0,1,1,1 }, _dy[8] = { -1,0,1,-1,1,-1,0,1 };
int x, y;
int t, n, m;
vector<int> G[510];
bool judge;
bool used[510];
void dfs(int u, int fa)
{
used[u] = true;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (fa == v)continue;
if (used[v]) {
judge = true;
return;
}
dfs(v, u);
if (judge)return;
}
}
int main()
{
while (~scanf("%d", &t)) {
while (t--) {
judge = false; cl0(used);
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
G[i].clear();
for (int i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
G[x].push_back(y); G[y].push_back(x);
}
if (n != m + 1) {
puts("NO");
continue;
}
dfs(1, -1);
for (int i = 1; i <= n; i++) {
if (!used[i]) {
judge = true;
break;
}
}
if (judge) puts("NO");
else puts("YES");
}
}
return 0;
}
CodeChef #SCC5 : Celebrity In Trouble
传送门:https://www.codechef.com/problems/SCC5
题意:有n个人,如果一个人不认识任何其他的人,并且其他人都认识他,那么他被称作Celebrity,问是否存在这样的人。
思路:记入度和出度,入度为n-1并且出度为0的人就是。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<utility>
#include<algorithm>
#include<utility>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<map>
#include<ctime>
#include<functional>
#include<bitset>
#define P pair<int,int>
#define ll long long
#define ull unsigned long long
#define lson id*2,l,mid
#define rson id*2+1,mid+1,r
#define ls id*2
#define rs (id*2+1)
#define Mod(a,b) a<b?a:a%b+b
#define cl0(a) memset(a,0,sizeof(a))
#define cl1(a) memset(a,-1,sizeof(a))
using namespace std;
const ll M = 1e9 + 7;
const ll INF = 1e9;
const int N = 410;
const double _e = 10e-6;
const int maxn = 2e5;
const int matSize = 9;
const int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };
const int _dx[8] = { -1,-1,-1,0,0,1,1,1 }, _dy[8] = { -1,0,1,-1,1,-1,0,1 };
int x, y;
int t, n, m;
int in[1010], out[1010];
int main()
{
while (~scanf("%d", &t)) {
while (t--) {
bool judge = false;
cl0(in); cl0(out);
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &x, &y);
in[y]++; out[x]++;
}
for (int i = 1; i <= n; i++) {
if (in[i] == n - 1 && out[i] == 0) {
printf("alive %d\n", i);
judge = true;
break;
}
}
if (!judge)
puts("dead");
}
}
return 0;
}
POJ #1386 : Play on Words
传送门:http://poj.org/problem?id=1386
题意:给n个单词,问能否将它们连成一串,需要前面单词的最后一个字母和后面单词的第一个字母相同。
思路:统计每个字母出现在首尾的次数。
对于每个字母,只有可能:①首尾次数相同;②首位次数=尾巴次数+1(这个字母作为头);③尾巴次数=首位次数+1(这个字母作为尾);④|首位次数-尾巴次数|>=2(不可能连成串)
之后统计可以当做头和尾的字母数量。如果都为0或都为1,则进行dfs判断是否整个连通;否则压根不可能连成串。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<utility>
#include<algorithm>
#include<utility>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cmath>
#include<map>
#include<ctime>
#include<functional>
#include<bitset>
#define P pair<int,int>
#define ll long long
#define ull unsigned long long
#define lson id*2,l,mid
#define rson id*2+1,mid+1,r
#define ls id*2
#define rs (id*2+1)
#define Mod(a,b) a<b?a:a%b+b
#define cl0(a) memset(a,0,sizeof(a))
#define cl1(a) memset(a,-1,sizeof(a))
using namespace std;
const ll M = 1e9 + 7;
const ll INF = 1e9;
const int N = 410;
const double _e = 10e-6;
const int maxn = 2e5;
const int matSize = 9;
const int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 };
const int _dx[8] = { -1,-1,-1,0,0,1,1,1 }, _dy[8] = { -1,0,1,-1,1,-1,0,1 };
int x, y, t, n, m;
int head, back;
int start[27], endd[27], has[27];
vector<int> a[27];
bool used[27];
int maze[27][27];
char s[1010];
void dfs(int u)
{
used[u] = true;
for (int i = 0; i < a[u].size(); i++) {
int v = a[u][i];
if (!used[v])
dfs(v);
}
}
int main()
{
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
cl0(start); cl0(endd); cl0(used); cl0(maze); cl0(has);
for (int i = 0; i < 26; i++)
a[i].clear();
head = 0; back = 0;
for (int i = 1; i <= n; i++) {
scanf("%s", s);
int len = strlen(s), l = s[0] - 'a', r = s[len - 1] - 'a';
if (maze[l][r] == 0) {
a[l].push_back(r);
maze[l][r] = 1;
}
has[l] = 1; has[r] = 1;
start[l]++; endd[r]++;
}
int st;
for (int i = 0; i < 26; i++)
if (has[i] == 1) {
st = i;
break;
}
bool judge = true;
for (int i = 0; i < 26; i++) {
if (start[i] != endd[i]) {
if (start[i] == endd[i] + 1) {
head++;
st = i;
}
else if (endd[i] == start[i] + 1)
back++;
else {
judge = false;
break;
}
}
}
if (!judge) {
puts("The door cannot be opened.");
continue;
}
judge = false;
if ((head == 0 && back == 0) || (head == 1 && back == 1)) {
judge = true; dfs(st);
for (int i = 0; i < 26; i++) {
if (has[i] == 1 && !used[i]) {
judge = false;
break;
}
}
}
if (judge)
puts("Ordering is possible.");
else
puts("The door cannot be opened.");
}
return 0;
}