UVa 11234 The Largest Clique
找最长的连接的点的数量。用tarjan缩点,思考可知每一个强连通分量里的点要么都选,要么都不选(走别的路),可以动规解决。
- #include<iostream>
- #include<cstdio>
- #include<algorithm>
- #include<vector>
- #include<cstring>
- using namespace std;
- const int mxn=;
- int top,stack[mxn];
- bool inst[mxn];
- int cnt,dnow;
- int dfn[mxn],low[mxn];
- int belone[mxn];
- int ptcnt[mxn];
- int dp[mxn];
- vector<int> e[mxn];//邻接表
- vector<int> pt[mxn];//缩点后的点集
- void clear(){
- cnt=;dnow=;top=;
- memset(dfn,-,sizeof(dfn));
- memset(inst,false,sizeof(inst));
- memset(dp,,sizeof dp);
- memset(ptcnt,,sizeof ptcnt);
- memset(belone,,sizeof belone);
- for(int i=;i<mxn;i++) e[i].clear();
- for(int i=;i<mxn;i++) pt[i].clear();
- }
- int n,m;
- void tarjan(int s){
- int v=,i;
- dfn[s]=++dnow;
- low[s]=dfn[s];
- inst[s]=true;
- stack[++top]=s;
- int si=e[s].size();
- for(i=;i<si;i++){
- v=e[s][i];
- if(dfn[v]==-){
- tarjan(v);
- low[s]=min(low[v],low[s]);
- }
- else if(inst[v]){
- low[s]=min(dfn[v],low[s]);
- }
- }
- if(dfn[s]==low[s]){
- cnt++;
- do{
- v=stack[top--];
- belone[v]=cnt;
- inst[v]=false;
- }while(s!=v);
- }
- return;
- }
- int find(int x){//动规
- if(pt[x].size()==)return dp[x]=ptcnt[x];
- if(dp[x])return dp[x];
- int mx=;
- for(int i=;i<pt[x].size();i++){
- mx=max(mx,find(pt[x][i]));
- }
- dp[x]=mx+ptcnt[x];
- return dp[x];
- }
- void solve(){//统计缩完点之后的连通情况
- int i,j,k;
- for(i=;i<=n;i++){
- ptcnt[belone[i]]++;
- for(j=;j<e[i].size();j++){
- int v=e[i][j];
- if(belone[i]!=belone[v])
- pt[belone[i]].push_back(belone[v]);
- }
- }
- int ans=;
- for(i=;i<=cnt;i++)ans=max(ans,find(i));
- printf("%d\n",ans);
- return;
- }
- int main(){
- int T;
- scanf("%d",&T);
- while(T--){
- scanf("%d%d",&n,&m);
- clear();
- int i,j;
- int u,v;
- for(i=;i<=m;i++){
- scanf("%d%d",&u,&v);
- e[u].push_back(v);
- }
- for(i=;i<=n;i++){//缩点
- if(dfn[i]==-)tarjan(i);
- }
- solve();
- }
- return ;
- }
UVa 11234 The Largest Clique的更多相关文章
- UVA 11324 - The Largest Clique(强连通分量+缩点)
UVA 11324 - The Largest Clique 题目链接 题意:给定一个有向图,要求找一个集合,使得集合内随意两点(u, v)要么u能到v,要么v能到u,问最大能选几个点 思路:强连通分 ...
- uva 11324 The Largest Clique(图论-tarjan,动态规划)
Problem B: The Largest Clique Given a directed graph G, consider the following transformation. First ...
- uva 11324 The Largest Clique
vjudge 上题目链接:uva 11324 scc + dp,根据大白书上的思路:" 同一个强连通分量中的点要么都选,要么不选.把强连通分量收缩点后得到SCC图,让每个SCC结点的权等于它 ...
- uva 11324 The Largest Clique(强连通分量缩点+DAG动态规划)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=sh ...
- UVA 1324 The Largest Clique 最大团(强连通分量,变形)
题意:给一个有向图,要求找出一些点,使得这些点中的任意点对,要么可以互通,要么单向可达. 思路:最低只要求单向可达即可,即a->b都可以算进去. 强连通分量内的点肯定是满足要求的,可以全选,但是 ...
- uva 11324 The Largest Clique (Tarjan+记忆化)
/*每个环 要么不选 要么全选 可缩点 就得到一个GAD图 然后搞搞算出最大路径*/ #include<iostream> #include<cstdio> #include& ...
- UVA - 11324 The Largest Clique 强连通缩点+记忆化dp
题目要求一个最大的弱联通图. 首先对于原图进行强连通缩点,得到新图,这个新图呈链状,类似树结构. 对新图进行记忆化dp,求一条权值最长的链,每一个点的权值就是当前强连通分量点的个数. /* Tarja ...
- UVA 11324 The Largest Clique(强连通分量+缩点DAG的DP)
题意:给定一个有向图,求出一个最大的结点集,这个节点集中的随意两个点之间至少一个能到达还有一个点. 思路:假设一个点在这个节点集中,那么它所在的强连通分量中的点一定所有在这个节点集中,反之亦然, 求出 ...
- UVA 11324.The Largest Clique tarjan缩点+拓扑dp
题目链接:https://vjudge.net/problem/UVA-11324 题意:求一个有向图中结点数最大的结点集,使得该结点集中任意两个结点u和v满足:要目u可以到达v,要么v可以到达u(相 ...
随机推荐
- python的对数
python的对数 首先要导入 math 模块: import math import numpy as np math.log(8,2),此为以2为底8的对数 等于 math.log2(8); 等于 ...
- Django 模型与 Mysql 数据类型对应
Django 1.11.9 文件路径:site-packages\django\db\backends\mysql\base.py–class DatabaseWrapper _data_types ...
- 2018.10.30 NOIp模拟赛T2 数字对
[题目描述] 小 H 是个善于思考的学生,现在她又在思考一个有关序列的问题. 她的面前浮现出一个长度为 n 的序列{ai},她想找出一段区间[L, R](1 <= L <= ...
- 与SVN相关的程序的调试问题【转】
解决eclipse中出现Resource is out of sync with the file system问题. 分析:有时候因为时间紧迫的原因,所以就没去管它,今天再次遇到它,实在看着不爽,所 ...
- mysql的性能优化案例
在一次项目实现中,以前写了个程序,将在txt文件中的电话号码和对应的类型往数据库中插入,小数据量的情况下,用个数组遍历循环的方式,很容易解决,但是当数据量一下 但是,几十万个电话一次性插入,就变得耗时 ...
- [译]The Python Tutorial#2. Using the Python Interpreter
[译]The Python Tutorial#Using the Python Interpreter 2.1 Invoking the Interpreter Python解释器通常安装在目标机器的 ...
- [Poj3281]Dining(最大流)
Description 有n头牛,f种食物,d种饮料,每头牛有nf种喜欢的食物,nd种喜欢的饮料,每种食物如果给一头牛吃了,那么另一个牛就不能吃这种食物了,饮料也同理,问最多有多少头牛可以吃到它喜欢的 ...
- 笔记-python-tutorial-5.data structure
笔记-python-tutorial-5.data structure 1. data structure 1.1. list operation list.append(x) #尾部 ...
- Android引导页过多导致OOM内存泄漏
摘要:前几天推广我们APP的时候,有些手机加载引导页的时候会闪退或崩溃,在Bugly显示是OOM异常. 然后Bugly上面显示的解决方案是: 该异常表示未能成功分配字节内存,通常是因为内存不足导 ...
- A JavaScript Image Gallery
childNodes property: The childNodes property is a way of getting information about the children of ...