poj3417(LCA 어플리케이션)

Network
Time Limit: 2000MS
 
Memory Limit: 65536K
Total Submissions: 2496
 
Accepted: 726
Description
Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has just received a bad news which denotes that DxtNetwork(DN), the SN's business rival, intents to attack the network of SN. More unfortunately, the original network of SN is so weak that we can just treat it as a tree. Formally, there are N nodes in SN's network, N-1 bidirectional channels to connect the nodes, and there always exists a route from any node to another. In order to protect the network from the attack, Yixght builds M new bidirectional channels between some of the nodes.
As the DN's best hacker, you can exactly destory two channels, one in the original network and the other among the M new channels. Now your higher-up wants to know how many ways you can divide the network of SN into at least two parts.
Input
The first line of the input file contains two integers: N (1 ≤ N ≤ 100 000), M (1 ≤ M ≤ 100 000) — the number of the nodes and the number of the new channels.
Following N-1 lines represent the channels in the original network of SN, each pair (a,b) denote that there is a channel between node a and node b.
Following M lines represent the new channels in the network, each pair (a,b) denote that a new channel between node a and node b is added to the network of SN.
Output
Output a single integer — the number of ways to divide the network into at least two parts.
Sample Input
4 1
1 2
2 3
1 4
3 4

Sample Output
3

Source
POJ Monthly--2007.10.06, Yang Mu
제목:http://poj.org/problem?id=3417
분석: 이 문제는 곧 통계적으로 테두리가 링으로 덮인 횟수를 생각할 수 있다. 덮인 횟수가 0인 테두리는 다리이고 답은 +m이며 덮어쓴 횟수는 1이다. 이것은 새로운 테두리에 속하는 링이고 답은 +1이다.공공조상의 아버지뻘은 자식뻘의 고리에 없기 때문에 새로 추가된 공공조상의 커버 횟수는 모두 -2...얼떨떨하니 코드를 자세히 보세요~
새로운 가변에 x=y가 존재하는 상황에 대해wa를 판정하지 못했어요~~
코드:
#include
using namespace std;
const int mm=444444;
const int mn=111111;
int t[mm],p[mm];
int h[mn]={0},q[mn]={0},f[mn],mk[mn]={0};
bool vis[mn]={0};
int i,j,k,n,m,e,ans=0;
inline void add(int u,int v,int h[])
{
    t[e]=v,p[e]=h[u],h[u]=e++;
    t[e]=u,p[e]=h[v],h[v]=e++;
}
int find(int x)
{
    if(f[x]==x)return x;
    return f[x]=find(f[x]);
}
void tarjan(int u)
{
    int i,v;
    vis[f[u]=u]=1;
    for(i=q[u];i;i=p[i])
        if(vis[v=t[i]])mk[find(v)]-=2;
    for(i=h[u];i;i=p[i])
        if(!vis[v=t[i]])
        {
            tarjan(v),f[v]=u,mk[u]+=mk[v];
            if(mk[v]==1)++ans;
            if(mk[v]==0)ans+=m;
        }
}
void get(int &a)
{
    char c;
    while((c=getchar())'9');
    for(a=0;c>='0'&&c<='9';c=getchar())a=a*10+c-'0';
}
int main()
{
    get(n),get(m);
    for(e=k=1;k

좋은 웹페이지 즐겨찾기