UVa 11324 The Largest Clique (强连通分量+DP)

Posted dwtfukgv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVa 11324 The Largest Clique (强连通分量+DP)相关的知识,希望对你有一定的参考价值。

题意:给定一个有向图,求一个最大的结点集,使得任意两个结点,要么 u 能到 v,要么 v 到u。

析:首先,如果是同一个连通分量,那么要么全选,要么全不选,然后我们就可以先把强连通分量先求出来,然后缩成一个点,然后该图就成了一个DAG,然后就可以直接用DP来做了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e15;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 50;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}

vector<int> G[maxn];
int pre[maxn], lowlink[maxn], sccno[maxn];
int dfs_cnt, scc_cnt;
stack<int> S;

void dfs(int u){
  pre[u] = lowlink[u] = ++dfs_cnt;
  S.push(u);
  for(int i = 0; i < G[u].sz; ++i){
    int v = G[u][i];
    if(!pre[v]){
      dfs(v);
      lowlink[u] = min(lowlink[u], lowlink[v]);
    }
    else if(!sccno[v])
      lowlink[u] = min(lowlink[u], pre[v]);
  }
  if(lowlink[u] == pre[u]){
    ++scc_cnt;
    while(1){
      int x = S.top();  S.pop();
      sccno[x] = scc_cnt;
      if(x == u)  break;
    }
  }
}

void find_scc(int n){
  dfs_cnt = scc_cnt = 0;
  ms(pre, 0);  ms(sccno, 0);
  for(int i = 1; i <= n; ++i)
    if(!pre[i])  dfs(i);
}

vector<int> g[maxn];
int num[maxn];

int dp[maxn];
int ans;
bool vis[maxn];

void dfs1(int u){
  dp[u] = 0;
  for(int i = 0; i < g[u].sz; ++i){
    int v = g[u][i];
    dfs1(v);
    dp[u] = max(dp[u], dp[v]);
  }
  dp[u] += num[u];
  ans = max(ans, dp[u]);
}

int main(){
  int T;  cin >> T;
  while(T--){
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; ++i)  G[i].cl, g[i].cl;
    for(int i = 0; i < m; ++i){
      int u, v;
      scanf("%d %d", &u, &v);
      G[u].pb(v);
    }
    find_scc(n);  ms(num, 0);
    ms(vis, 0);
    for(int i = 1; i <= n; ++i)  ++num[sccno[i]];
    for(int i = 1; i <= n; ++i)
      for(int j = 0; j < G[i].sz; ++j){
        int v = G[i][j];
        if(sccno[i] != sccno[v]){
          g[sccno[i]].pb(sccno[v]);
          vis[sccno[v]] = 1;
        }
      }
    ans = 0;
    for(int i = 1; i <= scc_cnt; ++i)
      if(!vis[i])  dfs1(i);
    printf("%d\n", ans);
  }
  return 0;
}

  

以上是关于UVa 11324 The Largest Clique (强连通分量+DP)的主要内容,如果未能解决你的问题,请参考以下文章

UVA11324 The Largest Clique —— 强连通分量 + 缩点 + DP

The Largest Clique UVA - 11324 (有向图最大团)

UVA11324 The Largest Clique[强连通分量 缩点 DP]

UVA 11324.The Largest Clique tarjan缩点+拓扑dp

UVa 11324 The Largest Clique (强连通分量+DP)

UVA-11324 The Largest Clique 有向图强连通+缩点+DP