POJ 1094 Sorting It All Out

题目链接:https://vjudge.net/problem/POJ-1094

题目大意

   给定 N 个变量的 M 个大小关系,依次处理,判断这 N 个变量的大小关系能否确定?如能确定,在处理第几个大小关系时检测成功?如不能确定,是否有矛盾?如有矛盾,在处理第几个大小关系时检测出了矛盾?

分析

  每输入一行就进行一次拓扑排序,暴力求解即可。

代码如下

  1 #include <cmath>
  2 #include <ctime>
  3 #include <iostream>
  4 #include <string>
  5 #include <vector>
  6 #include <cstdio>
  7 #include <cstdlib>
  8 #include <cstring>
  9 #include <queue>
 10 #include <map>
 11 #include <set>
 12 #include <algorithm>
 13 #include <cctype>
 14 #include <stack>
 15 #include <deque>
 16 #include <list>
 17 #include <sstream>
 18 #include <cassert>
 19 using namespace std;
 20  
 21 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 22 #define Rep(i,n) for (int i = 0; i < (n); ++i)
 23 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
 24 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
 25 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
 26 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
 27 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 28 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 29  
 30 #define pr(x) cout << #x << " = " << x << "  "
 31 #define prln(x) cout << #x << " = " << x << endl
 32  
 33 #define LOWBIT(x) ((x)&(-x))
 34  
 35 #define ALL(x) x.begin(),x.end()
 36 #define INS(x) inserter(x,x.begin())
 37 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
 38 #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c 
 39 #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
 40 #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
 41  
 42 #define ms0(a) memset(a,0,sizeof(a))
 43 #define msI(a) memset(a,0x3f,sizeof(a))
 44 #define msM(a) memset(a,-1,sizeof(a))
 45 
 46 #define MP make_pair
 47 #define PB push_back
 48 #define ft first
 49 #define sd second
 50  
 51 template<typename T1, typename T2>
 52 istream &operator>>(istream &in, pair<T1, T2> &p) {
 53     in >> p.first >> p.second;
 54     return in;
 55 }
 56  
 57 template<typename T>
 58 istream &operator>>(istream &in, vector<T> &v) {
 59     for (auto &x: v)
 60         in >> x;
 61     return in;
 62 }
 63  
 64 template<typename T1, typename T2>
 65 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
 66     out << "[" << p.first << ", " << p.second << "]" << "
";
 67     return out;
 68 }
 69 
 70 inline int gc(){
 71     static const int BUF = 1e7;
 72     static char buf[BUF], *bg = buf + BUF, *ed = bg;
 73     
 74     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
 75     return *bg++;
 76 } 
 77 
 78 inline int ri(){
 79     int x = 0, f = 1, c = gc();
 80     for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
 81     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
 82     return x*f;
 83 }
 84 
 85 template<class T>
 86 inline string toString(T x) {
 87     ostringstream sout;
 88     sout << x;
 89     return sout.str();
 90 }
 91 
 92 inline int toInt(string s) {
 93     int v;
 94     istringstream sin(s);
 95     sin >> v;
 96     return v;
 97 }
 98 
 99 //min <= aim <= max
100 template<typename T>
101 inline bool BETWEEN(const T aim, const T min, const T max) {
102     return min <= aim && aim <= max;
103 }
104  
105 typedef long long LL;
106 typedef unsigned long long uLL;
107 typedef pair< double, double > PDD;
108 typedef pair< int, int > PII;
109 typedef pair< int, PII > PIPII;
110 typedef pair< string, int > PSI;
111 typedef pair< int, PSI > PIPSI;
112 typedef set< int > SI;
113 typedef set< PII > SPII;
114 typedef vector< int > VI;
115 typedef vector< char > VC;
116 typedef vector< double > VD;
117 typedef vector< VI > VVI;
118 typedef vector< SI > VSI;
119 typedef vector< PII > VPII;
120 typedef map< int, int > MII;
121 typedef map< LL, int > MLLI;
122 typedef map< int, string > MIS;
123 typedef map< int, PII > MIPII;
124 typedef map< PII, int > MPIII;
125 typedef map< string, int > MSI;
126 typedef map< string, string > MSS;
127 typedef map< PII, string > MPIIS;
128 typedef map< PII, PII > MPIIPII;
129 typedef multimap< int, int > MMII;
130 typedef multimap< string, int > MMSI;
131 //typedef unordered_map< int, int > uMII;
132 typedef pair< LL, LL > PLL;
133 typedef vector< LL > VL;
134 typedef vector< VL > VVL;
135 typedef priority_queue< int > PQIMax;
136 typedef priority_queue< int, VI, greater< int > > PQIMin;
137 const double EPS = 1e-8;
138 const LL inf = 0x3fffffff;
139 const LL infLL = 0x3fffffffffffffffLL;
140 const LL mod = 1e9 + 7;
141 const int maxN = 1e2 + 7;
142 const LL ONE = 1;
143 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
144 const LL oddBits = 0x5555555555555555;
145 
146 struct Edge{
147     int from, to;
148 };
149 
150 istream& operator>> (istream& in, Edge &x) {
151     in >> x.from >> x.to;
152     return in;
153 }
154 
155 struct Vertex{
156     int in;
157     VI next;
158     
159     void clear() {
160         in = 0;
161         next.clear();
162     }
163 };
164 
165 int N, M, ans, type;
166 Vertex V[27];
167 vector< Edge > E;
168 string topo; // 存拓扑序列 
169 
170 void addEdge(Edge &x) {
171     V[x.from].next.PB(E.size());
172     ++V[x.to].in;
173     E.PB(x);
174 }
175 
176 // 返回 1 表示能确定顺序,0 表示不能确定,-1表示矛盾 
177 int TopoSort() {
178     stack< int > sk;
179     int ind[27];
180     topo.clear();
181     
182     For(i, 1, N) {
183         if(!V[i].in) sk.push(i);
184         ind[i] = V[i].in;
185     }
186     
187     bool check = true; // check大小关系是否都确定了 
188     while(!sk.empty()) {
189         if(sk.size() > 1) check = false;
190         int tmp = sk.top(); sk.pop();
191         topo.PB(tmp + 'A' - 1);
192         
193         Rep(i, V[tmp].next.size()) {
194             Edge &e = E[V[tmp].next[i]];
195             
196             --ind[e.to];
197             if(!ind[e.to]) sk.push(e.to);
198         }
199     }
200     
201     if(topo.size() < N) return -1;
202     if(!check) return 0;
203     return 1;
204 }
205 
206 int main(){
207     //freopen("MyOutput.txt","w",stdout);
208     //freopen("input.txt","r",stdin);
209     //INIT();
210     while(cin >> N >> M && N) {
211         bool flag = false; 
212         type = 0;
213         E.clear();
214         For(i, 1, N) V[i].clear();
215                 
216         For(i, 1, M) {
217             string tmp;
218             Edge t;
219             
220             cin >> tmp;
221             if(flag) continue;
222             
223             t.from = tmp[0] - 'A' + 1;
224             t.to = tmp[2] - 'A' + 1;
225             if(tmp[1] == '>') swap(t.from, t.to);
226             addEdge(t);
227             
228             int tt = TopoSort();
229             if(tt == 1 || tt == -1) {
230                 type = tt;
231                 ans = i;
232                 flag = true;
233             }
234         }
235         
236         if(type == -1) printf("Inconsistency found after %d relations.
", ans);
237         else if(type == 1) printf("Sorted sequence determined after %d relations: %s.
", ans, topo.c_str());
238         else printf("Sorted sequence cannot be determined.
", ans);
239     }
240     return 0;
241 }
View Code
原文地址:https://www.cnblogs.com/zaq19970105/p/11307830.html