https://vjudge.net/problem/UVA-11324

给定一张有向图G,求一个节点数目最大的节点集,使得该集合中的任意两个节点u和v满足:要么u可以到达v,要么v可以到达u(u,v相互可达也算满足),要求输出最大的节点数

Given a directed graph G, consider the following transformation. First, create a new graph T(G) to have the same vertex set as G. Create a directed edge between two vertices u and v in T(G) if and only if there is a path between u and v in G that follows the directed edges only in the forward direction. This graph T(G) is often called the transitive closure of G. We define a clique in a directed graph as a set of vertices U such that for any two vertices u and v in U, there is a directed edge either from u to v or from v to u (or both). The size of a clique is the number of vertices in the clique.

Input The number of cases is given on the first line of input. Each test case describes a graph G. It begins with a line of two integers n and m, where 0 ≤ n ≤ 1000 is the number of vertices of G and 0 ≤ m ≤ 50, 000 is the number of directed edges of G. The vertices of G are numbered from 1 to n. The following m lines contain two distinct integers u and v between 1 and n which define a directed edge from u to v in G.

Output For each test case, output a single integer that is the size of the largest clique in T(G).

Sample Input 1 5 5 1 2 2 3 3 1 4 1 5 2

Sample Output 4

 #include<iostream>
 #include<cstdio>
 #include<vector>
 #include<cstring>
 using namespace std;
 ;
 struct Edge{
     int go,next;
 };
 struct InputEdge{
     int from,to;
 };
 int T,a,b,n,m,book[maxn],bcc_count,v[maxn],end[maxn],end2[maxn],map[maxn][maxn],bcc_node[maxn],mem[maxn];
 vector<int> G[maxn],G2[maxn];
 vector<InputEdge> inputedge;
 vector<int> S;
 void init(){
     memset(v,,sizeof(v));
     memset(book,,sizeof(book));
     bcc_count=;
     //memset(end,0,sizeof(end));
     //memset(end2,0,sizeof(end2));
     S.clear();
     ;i<=n;i++){
         G[i].clear();
         G2[i].clear();
     }
     memset(map,,sizeof(map));
     inputedge.clear();
     memset(bcc_node,,sizeof(bcc_node));
     memset(mem,,sizeof(mem));
 }
 void add(int from,int to){
     //Edge e;e.go=to;e.next=end[from];G.push_back(e);end[from]=G.size()-1;
     G[from].push_back(to);
 }
 void add2(int from,int to){
     //Edge e;e.go=to;e.next=end2[from];G2.push_back(e);end2[from]=G2.size()-1;
     G2[from].push_back(to);
 }
 void dfs(int u){
     v[u]=;
     ;i<G[u].size();i++){
         //int go=G[i].go;
         int go=G[u][i];
         if(!v[go]) dfs(go);
     }
     S.push_back(u);
 }
 void dfs2(int u){
     book[u]=bcc_count;
     bcc_node[bcc_count]++;
     ;i<G2[u].size();i++){
         //int go=G2[i].go;
         int go=G2[u][i];
         if(!book[go]) dfs2(go);
     }
 }
 int dp(int x){
     if(mem[x]) return mem[x];
     int& ans=mem[x];
     ;i<=bcc_count;i++) if(i!=x&&map[i][x]) ans=max(ans,dp(i)+bcc_node[x]);
     if(!ans) ans=bcc_node[x];
     return ans;
 }
 int main()
 {
     scanf("%d",&T);
     while(T--){
         scanf("%d %d",&n,&m);
         init();
         ;i<=m;i++){
             scanf("%d %d",&a,&b);
             add(a,b);add2(b,a);
             inputedge.push_back((InputEdge){a,b});
         }
         ;i<=n;i++) if(!v[i]) dfs(i);
         ;i>=;i--) if(!book[S[i]]){
             bcc_count++;
             dfs2(S[i]);
         }
         ){
             printf("%d\n",n);
             continue;
         }
         ;i<inputedge.size();i++){
             InputEdge& e=inputedge[i];
             map[book[e.;
         }
         ;
         ;i<=bcc_count;i++)
             ans=max(ans,dp(i));
         printf("%d\n",ans);
     }
     ;
 }

用邻接表存图求BCC会出错....

UVa11324 最大团 The Largest Clique-有向图强连通分量&DP的更多相关文章

  1. UVa11324 The Largest Clique(强连通分量+缩点+记忆化搜索)

    题目给一张有向图G,要在其传递闭包T(G)上删除若干点,使得留下来的所有点具有单连通性,问最多能留下几个点. 其实这道题在T(G)上的连通性等同于在G上的连通性,所以考虑G就行了. 那么问题就简单了, ...

  2. UVA 11324 - The Largest Clique(强连通分量+缩点)

    UVA 11324 - The Largest Clique 题目链接 题意:给定一个有向图,要求找一个集合,使得集合内随意两点(u, v)要么u能到v,要么v能到u,问最大能选几个点 思路:强连通分 ...

  3. UVA 11324 The Largest Clique (强连通分量,dp)

    给出一个有向图,求一个最大的结点集合,任意两个点u,v.u可到达v或v可到达u. 一个强连通分量肯定一起选的.而且只能在一条路径上. 所以先找出所有scc,然后缩点找一条最大权的路径,按拓扑序跑DAG ...

  4. 有向图强连通分量的Tarjan算法

    有向图强连通分量的Tarjan算法 [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G ...

  5. 有向图强连通分量 Tarjan算法

    [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.非强连通图有向图的极 ...

  6. 【转】有向图强连通分量的Tarjan算法

    原文地址:https://www.byvoid.com/blog/scc-tarjan/ [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly con ...

  7. 图的连通性:有向图强连通分量-Tarjan算法

    参考资料:http://blog.csdn.net/lezg_bkbj/article/details/11538359 上面的资料,把强连通讲的很好很清楚,值得学习. 在一个有向图G中,若两顶点间至 ...

  8. 有向图强连通分量的Tarjan算法和Kosaraju算法

    [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.非强连通图有向图的极 ...

  9. 算法笔记_144:有向图强连通分量的Tarjan算法(Java)

    目录 1 问题描述 2 解决方案 1 问题描述 引用自百度百科: 如果两个顶点可以相互通达,则称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连 ...

随机推荐

  1. JavaScript中Call()以及Apply()的应用

    apply()和call()的真正用武之地是能够扩充函数赖以运行的作用域 三点说明: 1.每个函数都包含两个非继承而来的方法:apply()和call(). 2.他们的用途相同,都是在特定的作用域中调 ...

  2. NeHe OpenGL教程 第三十三课:TGA文件

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  3. python (2)xpath与定向爬虫

    内容来自:极客学院,教学视频: 写在前面: 提取Item 选择器介绍 我们有很多方法从网站中提取数据.Scrapy 使用一种叫做 XPath selectors的机制,它基于 XPath表达式. 这是 ...

  4. 使用Modernizr探测HTML5/CSS3新特性

    [转] HTML5, CSS3以及相关技术(例如canvas和web sockets)带来了非常有用的特性,可以让我们的web程序提升一个新的level.这些新技术允许我们只用HTML,CSS和Jav ...

  5. 手动编译Jsp文件

    手动模拟Tomcat编译jsp文件 Tomcat编译jsp文件的配置路径是在%tomcat_home%/conf/web.xml中,有这样一段代码 <servlet> <servle ...

  6. 如何利用svn自动同步更新到网站服务器

    我们最终的目的是:当本地提交后,SVN服务器自动更新服务器端指定WEB目录内的文件 实现方法: 找到服务器端 SVN版本库所在的目录(目录名称是Repositories),这个目录是在安装Visual ...

  7. 30天轻松学习javaweb_通过telnet连接http服务器

    telnet是windows自带的网络连接工具,可以用于连接任何服务器. 通过Telnet连接服务端 Telnet localhost 8080GET /news/1.html HTTP/1.1Hos ...

  8. 【转】深入理解DIP、IoC、DI以及IoC容器

    原文链接:http://www.cnblogs.com/liuhaorain/p/3747470.html 前言 对于大部分小菜来说,当听到大牛们高谈DIP.IoC.DI以及IoC容器等名词时,有没有 ...

  9. 新建一个mybatis HelloWorld

    1.下载mybatis https://github.com/mybatis/mybatis-3/ 没有梯子好像打不开 下载一个最新版本,我这里下载的是mybatis-3.4.1.zip 里面有myb ...

  10. angularJs自定义指令时的绑定

    <!DOCTYPE html> <html ng-app="app"> <head> <meta charset="utf-8& ...