POJ 1270 Following Orders

2021-05-20 08:28

阅读:649

标签:iis   void   之间   拓扑排序   链接   nbsp   space   cout   sign   

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

题目大意

   给定一些变量和某些变量之间的大小关系,按字典序输出所有可能的从小到大的变量拓扑序。

分析

  本质还是拓扑排序,只不过要稍微变化一下,字典序可以递归求解。

代码如下

技术图片技术图片
  1 #include   2 #include   3 #include   4 #include string>
  5 #include   6 #include   7 #include   8 #include   9 #include  10 #include  11 #include set>
 12 #include 
 13 #include  14 #include  15 #include  16 #include  17 #include  18 #include  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  23 #define For(i,s,t) for (int i = (s); 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  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  31 #define prln(x) cout  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 52 istream &operator>>(istream &in, pair &p) {
 53     in >> p.first >> p.second;
 54     return in;
 55 }
 56  
 57 template 58 istream &operator>>(istream &in, vector &v) {
 59     for (auto &x: v)
 60         in >> x;
 61     return in;
 62 }
 63  
 64 template 65 ostream &operatorout, const std::pair &p) {
 66     out "[" ", " "]" "\n";
 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(; c48||c>57; f = c==-?-1:f, c=gc());
 81     for(; c>47&&c58; x = x*10 + c - 48, c=gc());
 82     return x*f;
 83 }
 84 
 85 templateclass 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 
100 template101 inline bool BETWEEN(const T aim, const T min, const T max) {
102     return min  max;
103 }
104  
105 typedef long long LL;
106 typedef unsigned long long uLL;
107 typedef pairdouble, double > PDD;
108 typedef pairint, int > PII;
109 typedef pairint, PII > PIPII;
110 typedef pairstring, int > PSI;
111 typedef pairint, PSI > PIPSI;
112 typedef setint > SI;
113 typedef set SPII;
114 typedef vectorint > VI;
115 typedef vectorchar > VC;
116 typedef vectordouble > VD;
117 typedef vector VVI;
118 typedef vector VSI;
119 typedef vector VPII;
120 typedef mapint, int > MII;
121 typedef mapint > MLLI;
122 typedef mapint, string > MIS;
123 typedef mapint, PII > MIPII;
124 typedef mapint > MPIII;
125 typedef mapstring, int > MSI;
126 typedef mapchar, int > MCI;
127 typedef mapstring, string > MSS;
128 typedef mapstring > MPIIS;
129 typedef map MPIIPII;
130 typedef multimapint, int > MMII;
131 typedef multimapstring, int > MMSI;
132 //typedef unordered_map uMII;
133 typedef pair PLL;
134 typedef vector VL;
135 typedef vector VVL;
136 typedef priority_queueint > PQIMax;
137 typedef priority_queueint, VI, greaterint > > PQIMin;
138 const double EPS = 1e-8;
139 const LL inf = 0x3fffffff;
140 const LL infLL = 0x3fffffffffffffffLL;
141 const LL mod = 1e9 + 7;
142 const int maxN = 1e2 + 7;
143 const LL ONE = 1;
144 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
145 const LL oddBits = 0x5555555555555555;
146 
147 struct Edge{
148     int from, to;
149 };
150 
151 istream& operator>> (istream& in, Edge &x) {
152     in >> x.from >> x.to;
153     return in;
154 }
155 
156 struct Vertex{
157     int in;
158     VI next;
159     
160     void clear() {
161         in = 0;
162         next.clear();
163     }
164 };
165 
166 int N;
167 Vertex V[21];
168 MCI dic; // VertexName -> VertexId 
169 vector E;
170 string topo; // 存拓扑序列 
171 
172 void addEdge(Edge &x) {
173     V[x.from].next.PB(E.size());
174     ++V[x.to].in;
175     E.PB(x);
176 }
177 
178 bool vis[21]; 
179 inline void dfs() {
180     if(topo.size() >= N) {
181         cout  endl;
182         return;
183     }
184     
185     MCI::iterator it = dic.begin();
186     while(it != dic.end()) {
187         if(!V[it->sd].in && !vis[it->sd]) {
188             vis[it->sd] = 1;
189             topo.PB(it->ft);
190             Rep(i, V[it->sd].next.size()) {
191                 Edge &e = E[V[it->sd].next[i]];
192                 --V[e.to].in;
193             }
194             
195             dfs();
196             
197             Rep(i, V[it->sd].next.size()) {
198                 Edge &e = E[V[it->sd].next[i]];
199                 ++V[e.to].in;
200             }
201             topo.erase(topo.size() - 1);
202             vis[it->sd] = 0;
203         }
204         ++it;
205     }
206 }
207 
208 void init() {
209     E.clear();
210     For(i, 1, N) V[i].clear();
211     dic.clear();
212     topo.clear();
213     ms0(vis);
214 }
215 
216 int main(){
217     //freopen("MyOutput.txt","w",stdout);
218     //freopen("input.txt","r",stdin);
219     //INIT();
220     string tmp;
221     while(getline(cin, tmp)) {
222         init();
223         N = (tmp.size() + 1) >> 1;
224         For(i, 1, N) dic[tmp[(i - 1) 1]] = i;
225         
226         getline(cin, tmp);
227         stringstream ss(tmp);
228         char x, y;
229         while(ss >> x >> y) {
230             Edge t;
231             t.from = dic[x];
232             t.to = dic[y];
233             addEdge(t);
234         }
235         
236         dfs();
237         cout  endl;
238     }
239     return 0;
240 }
View Code

 

POJ 1270 Following Orders

标签:iis   void   之间   拓扑排序   链接   nbsp   space   cout   sign   

原文地址:https://www.cnblogs.com/zaq19970105/p/11310099.html


评论


亲,登录后才可以留言!