| ||||||||||
| 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 | |||||||||
Re:WA了两天了啊In Reply To:WA了两天了啊 Posted by:skyencc at 2011-03-08 18:15:23 oh,shit
搞了两天原来是输入结束判断问题,天哪,就这么捉弄我!!!
#include <iostream>
#include <fstream>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <sstream>
using namespace std;
#define MAX_VNODE_NUM 60
typedef struct ArcNode
{
int v;
ArcNode *next;
} ArcNode;
typedef struct VNode
{
ArcNode * fstarc;
} VNode;
typedef struct ALGraph
{
VNode vertices[MAX_VNODE_NUM];
int arcnum, vexnum;
} ALGraph;
void insert(ALGraph *g, int v1, int v2)
{
ArcNode *p = new ArcNode();
p->next = NULL;
p->v = v2;
if(g->vertices[v1].fstarc==NULL)
g->vertices[v1].fstarc = p;
else
{
ArcNode *q = g->vertices[v1].fstarc;
if(v2<q->v)
{
p->next = q;
g->vertices[v1].fstarc = p;
}
else
{
ArcNode *prev = q; q = q->next;
while(q && q->v<v2)
{
prev = q; q = q->next;
}
p->next = q; prev->next = p;
}
}
}
void traverse(ALGraph *g, int k, bool *is_visited)
{
is_visited[k] = true;
ArcNode *p = g->vertices[k].fstarc;
cout<<"("<<k;
while(p)
{
if(!is_visited[p->v])
{
cout<<" ";
traverse(g, p->v, is_visited);
}
p = p->next;
}
cout<<")";
}
int main()
{
string s;
int *edge_count = new int[MAX_VNODE_NUM];
bool *is_visited = new bool[MAX_VNODE_NUM];
queue<int> que;
ALGraph *g = new ALGraph();
int i=0, j=0, v=0;
ArcNode *p;
while(getline(cin, s))
{
if(!(s[0]>='0'&&s[0]<='9')) {cout<<"(1)"<<endl; continue;}
stringstream ss(s);
memset(edge_count, 0, sizeof(int)*MAX_VNODE_NUM);
memset(is_visited, false, sizeof(bool)*MAX_VNODE_NUM);
for(int i=0; i<MAX_VNODE_NUM; i++)
g->vertices[i].fstarc = NULL;
while(ss>>v)
{
edge_count[v]++;
que.push(v);
}
g->vexnum = que.back();
for(j=1; j<g->vexnum; j++)
{
for(i=1; i<=g->vexnum; i++)
{
if(edge_count[i]==0)
{
edge_count[i] = -1;
edge_count[que.front()]--;
insert(g, i, que.front());
insert(g, que.front(), i);
que.pop();
break;
}
}
}
traverse(g, 1, is_visited);
cout<<endl;
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator