HDU 4263 (red/blue Spanning tree-take edge greedy)

Source: Internet
Author: User

Red/blue Spanning TreeTime limit:10000/2000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 979 Accepted Submission (s): 368


Problem Descriptiongiven an undirected, unweighted, connected graph, where each edge is colored either blue or red, determ Ine whether a spanning tree with exactly kBlue edges exists.
Inputthere'll is several test cases in the input. Each test case would begin with a line with three integers:
N M k
Where N(2≤ N≤1,000) is the number of nodes in the graph, m(limited by the structure of the graph) are the number of edges in the graph, and k(0≤ k< N) is the number of the blue edges desired in the spanning tree.
Each of the next mLines would contain three elements, describing the edges:
C F t
Where Cis a character, either capital ' R' or capital ' B', indicating the color of the edge, and Fand Tis integers (1≤ F, TN, TFindicating the nodes that edge goes from and to. The graph is guaranteed to being connected, and there is guaranteed to being at the most one edge between any pair of nodes.
The input would end with a line with three 0s.
Outputfor each test case, output single line, containing 1 if it's possible to build a spanning tree with exactly kBlue edges, and 0 if it is not possible. Output no extra spaces, and do not separate answers with blank lines.
Sample Input
3 3 2B 1 2B 2 3R 3 1 1R 1 20 0 0

Sample Output
10

Sourcethe University of Chicago Invitational programming Contest 2012
recommendliuyiding | We have carefully selected several similar problems for you:4257 4258 4260 4261 4262

Set Red edge to 2, Blue edge to 1, and MST to maximum blue edge number

The least number of blue edges to see if k is in the interval


#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < functional> #include <iostream> #include <cmath> #include <cctype> #include <ctime>using namespace std; #define for (I,n) for (int. i=1;i<=n;i++) #define FORK (I,k,n) for (int. i=k;i<=n;i++) #define REP (I,n) for (int i=0;i<n;i++) #define ForD (I,n) for (int. i=n;i;i--) #define REPD (I,n) for (int. i=n;i>=0;i--) #define FORP (x) for ( int p=pre[x];p; p=next[p]) #define LSON (x<<1) #define Rson ((x<<1) +1) #define MEM (a) memset (A,0,sizeof (a)); Define Memi (a) memset (A,127,sizeof (a)), #define MEMI (a) memset (A,128,sizeof (a)), #define INF (2139062143) #define F ( 100000007) #define MAXN (3*2000+10) #define MAXM (10000000+10) long long mul (long long A,long long B) {return (a*b)%F;} Long Long (long A,long long B) {return (a+b)%F;} Long Long sub (long A,long long B) {return (a-b+ (a)/f*f+f)%F;}    typedef long Long Ll;class bingchaji{public:int father[maxn],n; void Mem (inT _n) {n=_n;    for (i,n) father[i]=i;        } int Getfather (int x) {if (father[x]==x) return x;    Return Father[x]=getfather (Father[x]);        } void Unite (int x,int y) {father[x]=getfather (father[x]);        Father[y]=getfather (Father[y]);    Father[father[x]]=father[father[y]];    } bool Same (int x,int y) {return getfather (x) ==getfather (y);    }}s;int n,m,k;struct edge{int a,b,c; Edge () {} edge (int _x,int _y,int _c): A (_x), B (_y), C (_c) {} friend bool operator< (Edge A,edge b) {return A.C&LT;B.C;}    }e[maxm];int kr () {S.MEM (n);    Sort (e+1,e+1+m);    int ans1=0,t=0; for (I,M) if (!    S.same (e[i].a,e[i].b)) {s.unite (e[i].a,e[i].b), ans1+=e[i].c,++t;    cout<<e[i].a<< ' <<e[i].b<< ' <<e[i].c<<endl;    } if (t<n-1) return-1; return ans1;}    int main () {//Freopen ("G.in", "R", stdin);    Freopen (". Out", "w", stdout); while (cin>>n>>m>>k) {if (n+m+k==0) return 0;        S.mem (n);            for (i,m) {char c;int a,b,t;            scanf ("\n%c%d%d", &c,&a,&b);            if (c== ' B ') T=1;else t=2;        E[i]=edge (a,b,t);        } int t1=kr ();        for (i,m) e[i].c=3-e[i].c;        int T2=kr ();//cout<<t1<< ' <<t2<<endl;            if (t1!=-1) {t1=t1-(n-1);            T1=N-1-T1;  t2=t2-(n-1);            cout<<t1<< ' <<t2<<endl;                    if (t1>=k&&t2<=k) {cout<< "1\n";                Continue    }} cout<< "0\n"; } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 4263 (red/blue Spanning tree-take edge greedy)

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.