Test instructions: There is only one priest in a small town, and now some new people are going to get married and need a priest to preside over a ritual, giving the start time of each couple's wedding and the end time t, and the time they need for the ritual (the length of each couple may vary) d, the priest can start at the wedding D Within (s to s+d) or before the end of Time D (t-d to T) to complete the ritual. Now ask if you can give an arrangement that allows the priest to complete all the wedding ceremonies of the couple, and if so, output an arrangement.
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath > #include <vector> #include <queue> #include <stack> #include <map> #include <algorithm > #include <set>using namespace std;typedef long long ll;typedef unsigned long long Ull; #define MM (A, B) memset (A, B , sizeof (a)); const double EPS = 1e-10;const int inf =0x7f7f7f7f;const double pi=acos ( -1); const int Maxn=100+1000;int n,m,p Re[2*maxn],lowlink[2*maxn],dfs_clock,sccno[2*maxn],scc_cnt;int OPP[2*MAXN],ST[2*MAXN],ED[2*MAXN],TT[2*MAXN], indeg[2*maxn],color[2*maxn];struct node{int l,r;} Ne[2*maxn];vector<int> g[2*maxn],g[2*maxn];stack<int> s;void Tarjan (int u) {pre[u]=lowlink[u]=++dfs_ Clock S.push (U); for (int i=0;i<g[u].size (); i++) {int v=g[u][i]; if (!pre[v]) {Tarjan (v); Lowlink[u]=min (Lowlink[u],lowlink[v]); } else if (!sccno[v]) lowlink[u]=min (lowlink[U],PRE[V]); } if (Pre[u]==lowlink[u]) {scc_cnt++; for (;;) {int cur=s.top (); S.pop (); sccno[cur]=scc_cnt; if (u==cur) break; }}}bool FIND_SCC () {MM (pre,0); MM (sccno,0); MM (lowlink,0); dfs_clock=scc_cnt=0; for (int i=0;i<2*n;i++) if (!pre[i]) Tarjan (i); for (int i=0;i<2*n;i++) if (Sccno[i]==sccno[i^1]) return false; return true;} BOOL Inter (node A,node b) {return A.R>B.L&&A.L<B.R;} void Build () {for (int i=0;i<2*n;i++) g[i].clear (); for (int i=0;i<2*n;i++) for (int j=i+1;j<2*n;j++)//For any two points to be judged if (Inter (Ne[i],ne[j])) { G[i].push_back (j^1); G[j].push_back (i^1); }}void Topsort () {MM (indeg,0); MM (color,0); for (int i=1;i<=scc_cnt;i++) g[i].clear (); for (int i=0;i<2*n;i++) {opp[sccno[i]]=sccno[i^1]; Opp[sccno[i^1]]=sccno[i]; } for (int u=0;u<2*n;u++) for (int i=0;i<g[u].size (); i++) {int a=sccno[u],b=sccno[g[u][i] ]; if (a!=b) {indeg[a]++; G[b].push_back (a); }} queue<int> Q; for (int i=1;i<=scc_cnt;i++) if (!indeg[i]) Q.push (i); while (Q.size ()) {int U=q.front (); Q.pop (); if (!color[u]) {color[u]=1; Color[opp[u]]=-1; } for (int i=0;i<g[u].size (); i++) {int v=g[u][i]; indeg[v]--; if (!indeg[v]) Q.push (v); }} for (int i=0;i<n;i++) if (color[sccno[i<<1]]==1) printf ("%02d:%02d%02d:%02d\n",ne[i<< 1].L/60,NE[I<<1].L%60,NE[I<<1].R/60,NE[I<<1].R%60); else printf ("%02d:%02d%02d:%02d\n",ne[i<<1^1].l/60,ne[i<<1^1].l%60,ne[i<<1^1].r/60,ne[i< <1^1].R%60);} int main () {while (~scaNF ("%d", &n)) {for (int i=0;i<n;i++) {int a,b,c,d,e; scanf ("%d:%d%d:%d%d", &a,&b,&c,&d,&e); ne[i<<1].l=a*60+b; ne[i<<1].r=a*60+b+e; ne[i<<1^1].l=60*c+d-e; ne[i<<1^1].r=60*c+d; }//for each point, its inverse is the ^ relationship build (); if (FIND_SCC ()) {printf ("yes\n"); Topsort (); } else printf ("no\n"); } return 0;}
POJ 3683 father's wedding reception 2-sat+ output template