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;
}