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

1 文本格式

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e4 + 5;

const int maxk = 5005;

int n, k;

int idmaxn5;

char smaxn55, ansmaxk;

bool vismaxn;

struct Edge {

int v, nxt;

} emaxn \* 100;

int headmaxn, tot = 1;

void addedge(int u, int v) {

etot.v = v;

etot.nxt = headu;

headu = tot++;

}

int dfnmaxn, lowmaxn, colormaxn, stkmaxn, insmaxn, top, dfs_clock, c;

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

stk++top = x;

insx = 1;

dfnx = lowx = ++dfs_clock;

for (int i = headx; i; i = ei.nxt) {

int v = ei.v;

if (!dfnv) {

tarjan(v);

lowx = min(lowx, lowv);

} else if (insv)

lowx = min(lowx, dfnv);

}

if (dfnx == lowx) {

c++;

do {

colorstk\[top] = c;

insstk\[top] = 0;

} while (stktop-- != 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", &idij, sij);

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

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

if (j == k) continue;

int u = 2 * idij - (sij0 == 'B');

int v = 2 * idik - (sik0 == 'R');

addedge(u, v);

}

}

}

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

if (!dfni) tarjan(i);

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

if (colori == colori + 1) {

puts("-1");

return 0;

}

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

int f1 = colori, f2 = colori + 1;

if (visf1) {

ans(i + 1) \>\> 1 = 'R';

continue;

}

if (visf2) {

ans(i + 1) \>\> 1 = 'B';

continue;

}

if (f1 < f2) {

visf1 = 1;

ans(i + 1) \>\> 1 = 'R';

} else {

visf2 = 1;

ans(i + 1) \>\> 1 = 'B';

}

}

ansk + 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;
}
相关推荐
JieE21221 小时前
LeetCode 101. 对称二叉树|JS 递归 + 迭代双解法,彻底搞懂镜像判断
javascript·算法
JieE2122 天前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
Jack202 天前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法
小小杨树2 天前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
JieE2123 天前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE2123 天前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
vivo互联网技术3 天前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像
算法·大模型·cvpr·影像
Darling噜啦啦3 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
用户497863050733 天前
(一)小红的数组操作
算法·编程语言