C/C++,图算法——求强联通的Tarjan算法之源程序

1 文本格式

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e4 + 5;

const int maxk = 5005;

int n, k;

int id[maxn][5];

char s[maxn][5][5], ans[maxk];

bool vis[maxn];

struct Edge {

int v, nxt;

} e[maxn * 100];

int head[maxn], tot = 1;

void addedge(int u, int v) {

e[tot].v = v;

e[tot].nxt = head[u];

head[u] = tot++;

}

int dfn[maxn], low[maxn], color[maxn], stk[maxn], ins[maxn], top, dfs_clock, c;

void tarjan(int x) { // tarjan算法求强联通

stk[++top] = x;

ins[x] = 1;

dfn[x] = low[x] = ++dfs_clock;

for (int i = head[x]; i; i = e[i].nxt) {

int v = e[i].v;

if (!dfn[v]) {

tarjan(v);

low[x] = min(low[x], low[v]);

} else if (ins[v])

low[x] = min(low[x], dfn[v]);

}

if (dfn[x] == low[x]) {

c++;

do {

color[stk[top]] = c;

ins[stk[top]] = 0;

} while (stk[top--] != x);

}

}

int main() {

scanf("%d %d", &k, &n);

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= 3; j++) scanf("%d%s", &id[i][j], s[i][j]);

for (int j = 1; j <= 3; j++) {

for (int k = 1; k <= 3; k++) {

if (j == k) continue;

int u = 2 * id[i][j] - (s[i][j][0] == 'B');

int v = 2 * id[i][k] - (s[i][k][0] == 'R');

addedge(u, v);

}

}

}

for (int i = 1; i <= 2 * k; i++)

if (!dfn[i]) tarjan(i);

for (int i = 1; i <= 2 * k; i += 2)

if (color[i] == color[i + 1]) {

puts("-1");

return 0;

}

for (int i = 1; i <= 2 * k; i += 2) {

int f1 = color[i], f2 = color[i + 1];

if (vis[f1]) {

ans[(i + 1) >> 1] = 'R';

continue;

}

if (vis[f2]) {

ans[(i + 1) >> 1] = 'B';

continue;

}

if (f1 < f2) {

vis[f1] = 1;

ans[(i + 1) >> 1] = 'R';

} else {

vis[f2] = 1;

ans[(i + 1) >> 1] = 'B';

}

}

ans[k + 1] = 0;

printf("%s\n", ans + 1);

return 0;

}

2 代码格式

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 5;
const int maxk = 5005;

int n, k;
int id[maxn][5];
char s[maxn][5][5], ans[maxk];
bool vis[maxn];

struct Edge {
  int v, nxt;
} e[maxn * 100];

int head[maxn], tot = 1;

void addedge(int u, int v) {
  e[tot].v = v;
  e[tot].nxt = head[u];
  head[u] = tot++;
}

int dfn[maxn], low[maxn], color[maxn], stk[maxn], ins[maxn], top, dfs_clock, c;

void tarjan(int x) {  // tarjan算法求强联通
  stk[++top] = x;
  ins[x] = 1;
  dfn[x] = low[x] = ++dfs_clock;
  for (int i = head[x]; i; i = e[i].nxt) {
    int v = e[i].v;
    if (!dfn[v]) {
      tarjan(v);
      low[x] = min(low[x], low[v]);
    } else if (ins[v])
      low[x] = min(low[x], dfn[v]);
  }
  if (dfn[x] == low[x]) {
    c++;
    do {
      color[stk[top]] = c;
      ins[stk[top]] = 0;
    } while (stk[top--] != x);
  }
}

int main() {
  scanf("%d %d", &k, &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 3; j++) scanf("%d%s", &id[i][j], s[i][j]);

    for (int j = 1; j <= 3; j++) {
      for (int k = 1; k <= 3; k++) {
        if (j == k) continue;
        int u = 2 * id[i][j] - (s[i][j][0] == 'B');
        int v = 2 * id[i][k] - (s[i][k][0] == 'R');
        addedge(u, v);
      }
    }
  }

  for (int i = 1; i <= 2 * k; i++)
    if (!dfn[i]) tarjan(i);

  for (int i = 1; i <= 2 * k; i += 2)
    if (color[i] == color[i + 1]) {
      puts("-1");
      return 0;
    }

  for (int i = 1; i <= 2 * k; i += 2) {
    int f1 = color[i], f2 = color[i + 1];
    if (vis[f1]) {
      ans[(i + 1) >> 1] = 'R';
      continue;
    }
    if (vis[f2]) {
      ans[(i + 1) >> 1] = 'B';
      continue;
    }
    if (f1 < f2) {
      vis[f1] = 1;
      ans[(i + 1) >> 1] = 'R';
    } else {
      vis[f2] = 1;
      ans[(i + 1) >> 1] = 'B';
    }
  }
  ans[k + 1] = 0;
  printf("%s\n", ans + 1);
  return 0;
}
相关推荐
1 9 J17 分钟前
Java 上机实践4(类与对象)
java·开发语言·算法
passer__jw7672 小时前
【LeetCode】【算法】3. 无重复字符的最长子串
算法·leetcode
passer__jw7672 小时前
【LeetCode】【算法】21. 合并两个有序链表
算法·leetcode·链表
sweetheart7-72 小时前
LeetCode22. 括号生成(2024冬季每日一题 2)
算法·深度优先·力扣·dfs·左右括号匹配
lb36363636363 小时前
介绍一下数组(c基础)(详细版)
c语言
一丝晨光5 小时前
编译器、IDE对C/C++新标准的支持
c语言·开发语言·c++·ide·msvc·visual studio·gcc
景鹤5 小时前
【算法】递归+回溯+剪枝:78.子集
算法·机器学习·剪枝
_OLi_5 小时前
力扣 LeetCode 704. 二分查找(Day1:数组)
算法·leetcode·职场和发展
丶Darling.5 小时前
Day40 | 动态规划 :完全背包应用 组合总和IV(类比爬楼梯)
c++·算法·动态规划·记忆化搜索·回溯
风影小子5 小时前
IO作业5
算法