tarjan+縮點+樹的直徑
題意:給出n個點和m條邊的圖,存在重邊,問加一條邊以後,剩下的橋的數量最少為多少。
先tarjan縮點,再在這棵樹上求直徑。加的邊即是連線這條直徑的兩端。
1/*2view codetarjan+縮點+樹的直徑
3題意:給出n個點和m條邊的圖,存在重邊,問加一條邊以後,剩下的橋的數量最少為多少。
4先tarjan縮點,再在這棵樹上求直徑。加的邊即是連線這條直徑的兩端。5*/
6#pragma comment(linker, "/stack:1024000000,1024000000")
7 #include8 #include
9 #include10 #include11 #include12 #include13 #include14 #include15 #include16
using
namespace
std;
17 typedef long
long
ll;18
//typedef __int64 int64;
19const
int maxn = 300015;20
const
int maxm = 2000015;21
const
int inf = 0x7fffffff;22
const
double pi=acos(-1.0
);23
const
double eps = 1e-8;24
struct
edgeedge[ maxm<<1 ],edgetree[ maxm<<1
];27
intcnt,cnt2,head[ maxn ],head2[ maxn ];
28int
vis[ maxn ];
29int
dfn[ maxn ];
30int
low[ maxn ];
31int be[ maxn ];//
縮點32
int sum;//
the num of "縮點"
33int
id;34 stacksta;
35 queueq;
36int maxnode,maxs;//
這棵樹的直徑maxs
37int
dis[ maxn ];
38 mapmp;
39void
init()
52void addedge( int a,int
b )57
void addedge2( int a,int
b )62
63void tarjan( int u,int
link )
74else
if( link==v )
78else
if( vis[ v ]==1)81
}82if( dfn[u]==low[u] )92}
93}94void buildtree( int
n )100
}101
}102
}103
void bfs( int s,int
n )118
for( int i=head2[ cur ];i!=-1;i=edgetree[ i ].next )
125}
126return
;127
}128
int dfs(int u,int
p)
129
139 maxs=max(maxs,max1+max2);
140return
max1;
141 } //
dfs求樹的直徑 ok
142int
main()
156for( int i=1;i<=n;i++)
160}
161buildtree( n );
162 bfs( 1
,sum );
163bfs( maxnode,sum );
164//
printf("sum=%d, maxs=%d\n",sum,maxs);
165//
maxs = 0;
166//
dfs( 1,-1 );
167 printf("
%d\n
",sum-1-maxs);
168}
169return0;
170 }
hdu4612 縮點 樹的直徑
題目大意 求乙個連通圖然後加一條邊使得橋的數目最少 題解思路 先把橋兩邊不是的點所有連通的點都縮成乙個點 然後把縮完的點構成一顆樹那麼再直徑的兩端加一條邊就是最優方案 注意 判斷重邊 題目鏈結 include include include include include include inclu...
Tarjan縮點 SPFA 縮點
洛谷p3387縮點 tarjan spfa求dag上單源最短路模板題 用tarjan在原圖上求scc 縮點 用縮點之後的scc建乙個有向無環圖 scc權為此scc內所有點點權和 在新建的dag上將scc權視為邊權跑spfa 求scc 1 到scc n 的最長路即為所求答案 include inclu...
Tarjan演算法 縮點
我們這一篇是在已經了解tarjan演算法的基礎之上開始寫的,如果不了解的話,請先看大牛們 關於tarjan演算法的部落格。首先我們對於乙個有向無環的圖 dag 至少新增幾條邊才能使它變為強連通圖?我們很容易根據有向無環圖的性質得到,我們計算入度為零的點數為a,出度為零的點數為b,那麼我們至少需要新增...