#include <cstdio>#include<cstring>#include<queue>#include<vector>#include<iostream>using namespacestd;Const intMAXN =6010;Const intMAXM =5000000;structline{intID; intx1, y1; intx2, y2; Line (int_ID,int_X1,int_y1,int_X2,int_y2): ID (_id), X1 (_x1), Y1 (_y1), X2 (_x2), y2 (_y2) {}};structedge{intV, Next;} E[maxm];vector<line>Lim;intP[MAXN], Eid;intINDEGREE[MAXN];intN;voidinit () {memset (P,-1,sizeof(p)); memset (Indegree,0,sizeof(Indegree)); Eid=0;}voidInsertintUintv) {E[EID].V=v; E[eid].next=P[u]; P[u]= eid++;}intdirection (line A, line B) {if((a.x1<b.x1 && a.x2<b.x1) | | (b.x1<a.x1 && b.x2<a.x1))return 0; if(a.x1==a.x2)if(Max (A.y1,a.y2) <max (b.y1,b.y2))return 1; Else return 2; if(b.x1==b.x2)if(Max (B.y1,b.y2) <max (a.y1,a.y2))return 2; Else return 1; if(A.x1 <=b.x1) {if(A.x2 <=b.x2) {Doublet=b.y1+1.0* (b.y2-b.y1) * (a.x2-b.x1)/(b.x2-b.x1); //cout<<a.id<< "" <<b.id<< "Cao" <<a.y2<< "" <<t<<endl; if(A.y2 < T)return 1;//a->b Else return 2;//b->a; } Else { Doublet=a.y1+1.0* (a.y2-a.y1) * (b.x2-a.x1)/(a.x2-a.x1); if(B.y2 < T)return 2; Else return 1; } } Else { if(A.x2 <=b.x2) {Doublet=b.y1+1.0* (b.y2-b.y1) * (a.x2-b.x1)/(b.x2-b.x1); if(A.y2 < T)return 1;//a->b Else return 2;//b->a; } Else { Doublet=a.y1+1.0* (a.y2-a.y1) * (b.x2-a.x1)/(a.x2-a.x1); if(B.y2 < T)return 2; Else return 1; } }}voidbuild () {init (); intnum =lim.size (); for(inti =0; I < num-1; i++) { for(intj = i+1; J < num; J + +) {intrel =direction (Lim[i], lim[j]); //printf ("%d%d,%d\n", Lim[i].id, Lim[j].id, rel); Switch(rel) { Case 0: Break; Case 1: Insert (lim[i].id, lim[j].id); indegree[Lim[j].id]++; Break; Case 2: Insert (lim[j].id, lim[i].id); indegree[Lim[i].id]++; Break; } } }}voidtopo () {//queue<int> Q;priority_queue<int>Q; for(inti =1; I <= N; i++) { if(Indegree[i] = =0) {Q.push (i); } } while(!Q.empty ()) { //int now = Q.front (); intnow =Q.top (); printf ("%d", now); Q.pop (); for(inti = P[now]; I! =-1; i =E[i].next) { intv =e[i].v; INDEGREE[V]--; if(!Indegree[v]) {Q.push (v); } } }}intMain () {scanf ("%d", &N); Lim.clear (); for(inti =0; i < N; i++) { intX1,y1,x2,y2; scanf ("%d%d%d%d", &x1,&y1,&x2,&y2); if(x1>x2) {swap (X1,X2); Swap (Y1,Y2); } lim.push_back (Line (i+1, X1,y1,x2,y2)); } build (); Topo ();}
#include <iostream>#include<stdio.h>#include<queue>#include<string.h>#defineMAXN 10005#defineMAXM 20005using namespacestd;structedge{intV,next;} E[MAXM];intP[maxn],eid;intINDEGREE[MAXN];intN,m,get_point=0, Res;voidinit () {memset (P,-1,sizeof(p)); memset (Indegree,0,sizeof(Indegree)); Eid=0;}voidInsertintUintv) {E[EID].V=v; E[eid].next=P[u]; P[u]=eid++; INDEGREE[V]++;}inttopo () {intans=0; Queue<int>Q; Queue<int>Cost ; for(intI=1; i<=n; i++) { if(!Indegree[i]) {Q.push (i); Cost.push ( -); } } while(!Q.empty ()) {Get_point++; intnow=Q.front (); Q.pop (); intco=Cost.front (); Cost.pop (); Ans+=Co; for(intI=p[now]; i!=-1; I=E[i].next) { intv=e[i].v; INDEGREE[V]--; if(!Indegree[v]) {Q.push (v); Cost.push (Co+1); } } } returnans;}intMain () {intb; scanf ("%d%d",&n,&m); Init (); for(intI=1; i<=m; i++) {scanf ("%d%d",&a,&b); Insert (B,A); } Res=topo (); if(get_point==n) printf ("%d\n", RES); Elseprintf"unhappy!\n"); return 0;}
Two examples of topological sequencing