[Vijos] 1892 maximum matching on the tree (tree DP + count)

Source: Internet
Author: User

Https://vijos.org/p/1892

This must be card-tested machine + stack-enabled manually to pass Qaq.

Manually open the stack my Baidu...

`int size=256<<20; //256MBchar *p=(char*)malloc(size)+size;__asm__("movl %0, %%esp\n" :: "r"(p));`

Then I handed in numerous posts and then got stuck ....

We set the status

F [I] [0] indicates that neither the I node nor the son side is selected.

F [I] [1] indicates that the I node selects only one edge with the son.

G [I] [0] and G [I] [1] Solutions

So transfer is

F [I] [0] = Sigma {max {f [J] [0], F [J] [1]} J is the son of I}

F [I] [1] = max {f [J] [0] + Sigma {max {f [k] [0], f [k] [1]} both J and K are sons of I, J! = K}

The same is true for the solution.

In this way, we can avoid repeated mistakes...

For the latter, we use the prefix, suffix, and maintenance...

`#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>#include <queue>#include <set>#include <map>using namespace std;typedef long long ll;#define pii pair<int, int>#define mkpii make_pair<int, int>#define pdi pair<double, int>#define mkpdi make_pair<double, int>#define pli pair<ll, int>#define mkpli make_pair<ll, int>#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << (#x) << " = " << (x) << endl#define error(x) (!(x)?puts("error"):0)#define printarr2(a, b, c) for1(_, 1, b) { rep(__, c) cout << a[_][__] << ‘\t‘; cout << endl; }#define printarr1(a, b) for1(_, 1, b) cout << a[_] << ‘\t‘; cout << endlinline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }const int N=2000005;int ihead[N], cnt, n, f[N][2], st[N], r[N];ll MD, sumr[N], d[N][2];struct ED { int next, to; }e[N<<1];void add(int u, int v) {e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u;}void dp(int x, int fa) {int t0=0; ll s0=1;for(int i=ihead[x]; i; i=e[i].next) if(e[i].to!=fa) {int y=e[i].to;dp(y, x);int mx=max(f[y][0], f[y][1]); ll tp=0;if(f[y][0]==mx) tp+=d[y][0];if(f[y][1]==mx) tp+=d[y][1];s0=(s0*tp)%MD;t0+=mx;}f[x][0]=t0;d[x][0]=s0;int sz=0, l=0; ll suml=1;for(int i=ihead[x]; i; i=e[i].next) if(e[i].to!=fa) st[++sz]=e[i].to;sumr[sz+1]=1; r[sz+1]=0;for3(i, sz, 1) {int mx=max(f[st[i]][0], f[st[i]][1]); ll tp=0;if(mx==f[st[i]][0]) tp+=d[st[i]][0];if(mx==f[st[i]][1]) tp+=d[st[i]][1];r[i]=r[i+1]+mx;sumr[i]=(sumr[i+1]*tp)%MD;}f[x][1]=-N;for1(i, 1, sz) {int tp=l+f[st[i]][0]+r[i+1]+1;if(tp>f[x][1]) {f[x][1]=tp;d[x][1]=(d[st[i]][0]*((suml*sumr[i+1])%MD))%MD;}else if(tp==f[x][1]) {d[x][1]=d[x][1]+(d[st[i]][0]*((suml*sumr[i+1])%MD))%MD;}int mx=max(f[st[i]][0], f[st[i]][1]); s0=0;if(mx==f[st[i]][0]) s0+=d[st[i]][0];if(mx==f[st[i]][1]) s0+=d[st[i]][1];suml=(suml*s0)%MD;l+=mx;}}int main() {int size=256<<20;char *p=(char*)malloc(size)+size;__asm__("movl %0, %%esp\n" :: "r"(p));read(n);for1(i, 1, n-1) add(getint(), getint());read(MD);int root=(n+1)>>1;dp(root, -1);int ans1=max(f[root][0], f[root][1]); ll ans2=0;if(ans1==f[root][0]) ans2+=d[root][0];if(ans1==f[root][1]) ans2+=d[root][1];printf("%d\n%lld\n", ans1, ans2%MD);return 0;}`

Then you can ..

Description

Doc's school sister used to be an oier. She sometimes asked Doc various interesting competition questions.
Although it was very simple, it was a good memory for Doc.

Today, xuejie doesn't want to go out shopping because she saw a question and worried about it.

What is the maximum match on the tree? How many groups are there for the maximum matching solution?
(First, the tree can be regarded as an undirected graph G.
(For undirected graph G, the maximum match on it is a subset of the edge set, satisfying the following requirements:
(For each vertex in G, there is only one edge connected to it at most in this subset.
(The maximum matching is the maximum value that can be reached for this subset.

Format input format

Enter an integer N in the first line, indicating the number of nodes on the tree. All nodes are numbered 1 to n.
Next, there are n-1 rows. Each row has two integers, A and B, indicating that there is an edge of link nodes A and B.
The last row has an integer m, indicating that for the maximum matching solution, we only need to output the remainder of M.

Output Format

The first line has an integer indicating the maximum size of the matching.
The second row has an integer representing the total number of solutions after the remainder of M.

Example 1 input 1 [copy]
`51 23 24 51 417`
Sample output 1 [copy]
`23`
Restrictions

For 30% of data, n <= 2500.
For 70% of data, n <= 100,000.
For 100% of data, n <= 1,500,000.
M is a 32-bit signed integer.

[Vijos] 1892 maximum matching on the tree (tree DP + count)

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• Sales Support

1 on 1 presale consultation

• After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.