Codeforces #335div2 D. Lazy Student Construction

Source: Internet
Author: User

#include <bits/stdc++.h>#defineREP (I,A,B) for (int i=a;i<=b;i++)#defineMS0 (a) memset (A,0,sizeof (a))using namespaceStd;typedefLong Longll;Const intmaxn=1000100;Const intinf=1<< in;intn,m;structedge{intW is; intID; FriendBOOL operator<(Edge A,edge B) {returnA.W==B.W? A. is>b. is:a.w<B.W; }}; Edge E[MAXN],ANS[MAXN];BOOLCMP (Edge A,edge B) {returna.id<b.id;}intMain () {//freopen ("In.txt", "R", stdin);     while(cin>>n>>m) {REP (i,1, m) scanf ("%d%d", &e[i].w,&e[i]. is), e[i].id=i; Sort (e+1, e+m+1); intCur=1, cnt=0; intL=1, r=3; BOOLflag=1; REP (i,1, M) {            if(E[i]. is) {ans[++cnt]={cur,cur+1, e[i].id}; Cur++; }            Else{                BOOLtag=0; //cout<< "l=" <<L<< "r=" <<R<<endl;                if(r<=cur&&r-l>=2) tag=1; Else{                     while(r-l<=1&&r<=cur) {                         while(l>=1&&r-l<=1) l--; if(r-l>=2) {tag=1; Break; }                        Elser++; }                }                if(!tag) {Flag=0; Break; } ans[++cnt]={l,r,e[i].id}; L--; if(l<1) r++,l=r-2; }} sort (ans+1, ans+cnt+1, CMP); if(flag) {REP (I,1, CNT) printf ("%d%d\n", Ans[i].w,ans[i]. is); }        ElsePuts"-1"); }    return 0;}/** The structure of the topic is more free, although changeable, but there are some skills. The key to the construction is to avoid the tedious situation and find a case that is as simple and as close as possible to the answer and prove it. In this case, given some edge information (edge right and whether in MST), construct a matching diagram, if there is no output-1. Due to the vertex of the edge is not given, so the condition of the graph more than one, is obviously the construction of the topic. The pattern of construction is ever-changing, there is no fixed answer, but we need to find out the easy way to construct it as much as possible, which requires a preference for special cases. It is clear that MST is more easily structured than a tree, and as far as correctness is, it does not matter. Then, according to the nature of MST, it is natural to order Benquan by contacting Kruskal. This solution comes out: from small to large traversal edge, if it belongs to MST, directly added to the end of the chain, otherwise, in the chain to find two points, because the edge is from small to large add, so casually find even a point on the line, but should be careful not to have a heavy edge, and to avoid repeated traversal and leakage traversal, so the traversal The preceding commits are hung on the 27th because the traversal sequence is incorrect, causing some edges not to traverse, thus resulting in a situation of-1. Later, I thought of a solution, the original traversal is a direct window, now changed to L-left, R to the right, the first row of L and then r, so as to avoid the leakage traversal. V8 solution: or from small to large sort directly according to the degree of maintenance point, if not on the MST, take two degrees the smallest point, even edge, otherwise join a new point. Intermediate process each point has a degree of no more than cur-1 indicates a solution. (the degree of each point in a simple graph does not exceed n-1). This correctness is obvious,,,, Orz .... Update: The second solution needs to be sentenced to a heavy edge ... It's more troublesome to write .... is very troublesome ..... */
View Code

Codeforces #335div2 D. Lazy Student Construction

Contact Us

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.