Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
简单的面向对象c++ 用了大神提供的两组数据debug#include<iostream> #include <sstream> #include<string> #include<cstdlib> #include<cmath> #include<cstdio> #include<cctype> #include<stack> #include<fstream> #include<cassert> #include<map> #include<set> #include<vector> #include<cstring> #include<algorithm> #include"limits.h" using namespace std; struct Node { char c; int in; std::vector<int> out; Node(int cc):c(cc),in(0),out(){ } }; struct Graph { std::vector<Node> v; int size; std::vector<int> stat; std::vector<int> seq; Graph(int n); void buildGraph(const string & s); void judge(); bool sort(std::vector<int> & v); int locate(char c); void determine(); }; void Graph::determine() { int start=-1; for (int i = 0; i < stat.size(); ++i) { if (stat[i]==-1) { cout<<"Inconsistency found after "<<i+1<<" relations.\n"; return; } if (stat[i]==1&&start==-1) { start=i; cout<<"Sorted sequence determined after "<<start+1<<" relations: "; for (int i = 0; i < seq.size(); ++i) { cout<<v[seq[i]].c; } cout<<".\n"; return; } } if (start==-1||(stat.size()&&stat.back()==0)) { cout<<"Sorted sequence cannot be determined.\n"; return; } } int Graph::locate(char c) { for (int i = 0; i < v.size(); ++i) { if (v[i].c==c) { return i; } } return -1; } Graph::Graph(int n):size(n),v(),stat(){ } void Graph::buildGraph(const string & s) { int from=locate(s[0]); if (from==-1) { v.push_back(Node(s[0])); from=v.size()-1; } int to=locate(s[2]); if (to==-1) { v.push_back(Node(s[2])); to=v.size()-1; } v[from].out.push_back(to); v[to].in++; judge(); } bool Graph::sort(std::vector<int> & seq) { bool mark=0; for (int k = 0; k < v.size(); ++k) { int p=-1; for (int i = 0; i < v.size(); ++i) { if (v[i].in==0&&find(seq.begin(),seq.end(),i)==seq.end()) { if (p!=-1) { mark=1; //多选和有环 先判断有环 } p=i; } } if (p==-1) { return false; } seq.push_back(p); for(int i=v[p].out.size()-1;i>=0;i--) { int d=v[p].out[i]; v[d].in--; v[p].out.pop_back(); } } if (mark==1) { seq.pop_back(); return true; } return true; } void Graph::judge() { if (stat.size()&&stat.back()!=0) //确认和重复 优先重复 { return; } Graph tmp=*this; seq.clear(); bool s=tmp.sort(seq); if (s) { if (seq.size()==size) { stat.push_back(1); } else if (seq.size()<size) { stat.push_back(0); } assert(1); } else stat.push_back(-1); // cout<<"stat: "; // for (int i = 0; i < stat.size(); ++i) // { // cout<<stat[i]<<" "; // } // cout<<endl; } int main() { #ifndef ONLINE_JUDGE std::ios::sync_with_stdio(0); freopen("/Users/steven/Desktop/tmp.txt","r",stdin); clock_t a=clock(); #endif int n,m; while(cin>>m>>n) { if (!m&&!n) { break; } Graph g(m); while(n--) { string tmp; cin>>tmp; g.buildGraph(tmp); } g.determine(); } #ifndef ONLINE_JUDGE clock_t b=clock(); cout<<"running time:"<<(b-a)/(double)CLOCKS_PER_SEC<<endl; #endif } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator