[BZOJ4238] Voltage

Meaning of the questions:

You know Just Odd Inventions society do? The company's business is "just a wonderful invention (Just Odd Inventions)". Here referred to as the JOI community.
JOI society in a laboratory has a complex circuit. A resistor circuit nodes n and m are elongate composition. Nodes are numbered 1 ~ N
status of each node can have a set of high voltage [] or [] a low voltage. Each resistor connecting two nodes, only one end is a high voltage, the other end of the resistor is only a low voltage current flows. A high voltage or resistance at both ends of the low voltage no current flows.
One day, in order to maintain social JOI circuit, a resistor is selected, in order to allow only the current on the [root stop the flow resistance, the root of the other M-1 has a current flows through resistor], necessary to adjust the voltage of each node. In order to satisfy this condition, the resistance can select how many roots?
By the way, JOI club this wonderful circuit is used in the invention of what it? This is a top secret within the company, in addition to the president who did not know oh
now give information on the circuit, you can choose the number of resistance so that it does not flow when the output circuit maintenance.

Input:

The first line of two space-separated positive integers N and M, a circuit has N nodes and the M resistors.
Next M rows, the i-th row has two space-separated positive integers Ai and Bi (1 <= Ai <= N, 1 <= Bi <= N, Ai ≠ Bi), denotes the i th resistor and a connection node Ai node Bi.

Output:

Output line an integer representing the number of circuit resistance selectable maintenance when it does not flow.

Input:

4 4
1 2
2 3
3 2
4 3

Output:

2

Ideas:

First, the subject does not mean a resistor removed(Zz I should only be thought of)According to the meaning of the subject, if this resistance is selected, it should be the same at both ends of the voltage state.

We can be considered a high voltage, low voltage assumed to be zero, in order to meet the conditions to make this picture, which is adjacent to the color, can be understood as a bipartite graph.

So we consider under what circumstances it will not flop?

Even if some resistance into a ring, and just have an odd number of sides on the ring when certain contradictions (hand finished it can be determined) will appear. So we have to choose sides must appear on the odd ring, and to appear on all odd ring (or elsewhere will still be unsatisfied)

But on the odd ring not necessarily meet the conditions, if this edge also happens to appear on one or more of the even, to satisfy the conditions of these two even-ring must be a different color, inconsistent with the requirements of the same color .

So in fact we are looking for is present on all sides of the odd and does not appear on the even-numbered ring.

So how to update the ring on the side?

Here the difference can be considered a tree, a record array cnt, press dfs sequence determined whether this edge throwback side, if it is, then the current location cnt ++, cnt-- in the end position. For a x-> y side of it, as far as the value of cnt [y], is the contribution of this edge (side here refers to the side of the tree), as atavistic edge can be calculated directly when found.

ps: Here is a parity calculations separate, in fact, can be directly used in an array, even Qijia Save. Then might ask how to do if the ring does not exist, then all sides of the contribution is 0, and the total number is still the same odd ring 0, so too is the influence of Mo

#include<bits/stdc++.h>
#define M 200005
using namespace std;
void Rd(int &res) {
    res=0;
    char c;
    while(c=getchar(),c<48);
    do res=(res<<1)+(res<<3)+(c-'0');
    while(c=getchar(),c>=48);
}
int la[M],pr[M<<1],to[M<<1],tot,n,m,dfn[M],dep[M],id,Odd[M],Even[M],In[2][M],cnt;//这条边在奇环上 这条边在偶数环上
void add(int x,int y) {
    to[++tot]=y,pr[tot]=la[x],la[x]=tot;
}
struct node {
    int x,y;
} a[M];
bool mark[M<<1];
void dfs(int x) {
    dfn[x]=++id;
    for(int i=la[x]; i; i=pr[i]) {
        int y=to[i];
        if(!dfn[y]) {
            dep[y]=dep[x]+1,mark[i]=1;
            if(i&1)mark[i+1]=1;
            else mark[i-1]=1;
            dfs(y);
            Even[(i+1)/2]+=In[0][y],In[0][x]+=In[0][y];
            Odd[(i+1)/2]+=In[1][y],In[1][x]+=In[1][y];
        } else if(mark[i]||dfn[y]>dfn[x])continue;
        else if((dep[x]-dep[y])&1)In[0][x]++,In[0][y]--,Even[(i+1)/2]++;
        else In[1][x]++,In[1][y]--,Odd[(i+1)/2]++,cnt++;
    }
}
int ans;
int main() {
    Rd(n),Rd(m);
    for(int x,y,i=1; i<=m; i++)Rd(x),Rd(y),add(x,y),add(y,x),a[i]=(node)<%x,y%>;
    for(int i=1; i<=n; i++) {
        if(dfn[i])continue;
        dfs(i);
    }
    for(int i=1; i<=m; i++) {
        if(Even[i])continue;
        if(Odd[i]==cnt)ans++;
    }
    printf("%d\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/cly1231/p/11594445.html