POJ 3683 (2-SAT+SCC)

Source: Internet
Author: User

Portal: Problem 3683

Https://www.cnblogs.com/violet-acmer/p/9769406.html

Resources:

[1]: Challenge Program Design Contest

Test instructions

  There are n wedding, each wedding has the starting time Si, the end time Ti, there is a host ceremony to take Ti time, ti must be arranged at the beginning or end of the wedding.

The host is done by the priest, but there is only one priest, so the host time of each wedding can not be repeated, but the priest may attend a ceremony, immediately after the beginning of another start time and its end time equal to the ceremony, asked if you can normally arrange the host time, cannot output no, If you can, you need to output a specific answer: that is, the host time of each wedding in that time period.

Exercises

  For each wedding ceremony I, only at the beginning or the end of the ceremony, two options, so you can define the variable Xi;

Xi for true <=> at the beginning of the ceremony

Thus, for weddings I and J, if the Si~si+di and SJ~SJ+DJ conflict, there is a clause (non-XI V non-XJ) True, for the start and start, start and end, end and start, end and end respectively corresponding to four clauses, you can also get similar conditions.

So, to ensure that all ceremonies are not in conflict, just consider linking all these clauses with ^ (hop) to get the moral boolean formula is good.

For example, the Boolean formula you can get for an input sample is:

And when X1 is true and X2 is false, its value is true.

In this way, we turn the original problem into a 2-sat problem, and then we just have to do a strong connectivity component decomposition and determine if there is a set of Boolean variables that make the Boolean formula value TRUE.

The above analysis comes from the Challenge Program design competition P326.

Difficulties:

  Based on 2-sat, the strong connected component is decomposed by SCC

AC Code:

1#include <iostream>2#include <cstdio>3#include <vector>4#include <cstring>5#include <cmath>6 using namespacestd;7 #defineMem (A, B) (memset (a,b,sizeof a))8 #definePB Push_back9 Const intmaxn=1e3+ -;Ten  One //X1~xn:1~n A //non-x1~ non-xn:n+1~2*n - intN; - intS[MAXN]; the intT[MAXN]; - intD[MAXN]; -vector<int>g[2*maxn],rg[2*MAXN];//Note here is 2*MAXN -vector<int>vs; + intscc[2*MAXN]; - BOOLvis[2*MAXN]; + voidAddedge (intUintv) A { at G[U].PB (v); - RG[V].PB (u); - } - voidSAT () - { -      for(intI=1; I <= n;++i) in     { -          for(intj=1; J < i;++j) to         { +             if(Max (S[i],s[j]) < min (S[j]+d[j],s[i]+d[i]))//the opening time of the first wedding begins at the beginning of the first stage of the ceremony. -Addedge (I,j+n), Addedge (j,i+n); the             if(Max (S[i],t[j]-d[j]) < min (T[j],s[i]+d[i]))//the opening time of the first wedding ceremony and the end of the first J wedding. *Addedge (I,j), Addedge (j+n,i+n);//Note that the second place is J+n-I+n $             if(Max (S[j],t[i]-d[i]) < min (T[i],s[j]+d[j]))//at the end of the first stage of the wedding, the host time is in conflict with the start of section J wedding.Panax NotoginsengAddedge (I,j), Addedge (i+n,j+n);//Note that the second place is I+n-J+n -             if(Max (T[i]-d[i],t[j]-d[j]) < min (T[i],t[j]))//at the end of the first stage of the wedding, the host time is in conflict with the end of the section J wedding. theAddedge (I+n,j), Addedge (j+n,i); +         } A     } the } + voidDfs (intu) - { $vis[u]=true; $      for(intI=0; i < g[u].size (); + +i) -         if(!Vis[g[u][i]]) - Dfs (G[u][i]); the VS.PB (u); - }Wuyi voidRDfs (intUintk) the { -vis[u]=true; Wuscc[u]=K; -      for(intI=0; i < rg[u].size (); + +i) About     { $         intto=Rg[u][i]; -         if(!Vis[to]) - RDfs (to,k); -     } A } + voidSCC () the { -Mem (Vis,false); $ vs.clear (); the      for(intI=1; I <= n;++i) the         if(!Vis[i]) the Dfs (i); theMem (Vis,false); -     intk=0; in      for(intI=vs.size ()-1; I >=0;--i) the         if(!Vis[vs[i]]) theRDfs (vs[i],++k); About } the voidInit () the { the      for(intI=0; I < maxn;++i) + g[i].clear (), Rg[i].clear (); - } the intMain ()Bayi { the  the      while(~SCANF ("%d",&N)) -     { - Init (); the          for(intI=1; I <= n;++i) the         { the             inth,m; the             Charch; -scanf"%d%c%d",&h,&ch,&m); thes[i]=h* -+m;//Convert start time to minutes thescanf"%d%c%d",&h,&ch,&m); thet[i]=h* -+m;//Convert end time to minutes94scanf"%d", d+i); the         } theSAT ();//according to the 2-sat building map theSCC ();//strongly connected component decomposition98         BOOLflag=false; About          for(intI=1; I <= n;++i) -             if(Scc[i] = = scc[i+N])101flag=true;102         if(flag)103printf"no\n");104         Else the         {106printf"yes\n");107              for(intI=1; I <= n;++i)108             {109                 if(Scc[i] > scc[i+N]) theprintf"%02d:%02d%02d:%02d\n", s[i]/ -, s[i]% -, (S[i]+d[i])/ -, (S[i]+d[i])% -);111                 Else theprintf"%02d:%02d%02d:%02d\n", (T[i]-d[i])/ -, (T[i]-d[i])% -, t[i]/ -, t[i]% -);113             } the         } the     } the     return 0;117}
View Code

POJ 3683 (2-SAT+SCC)

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.