1097 - set<int>::iterator iter;...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include<cstdio> #include<set> #include<queue> #include<algorithm> using namespace std; typedef struct { int value; int parent; int freq; } Node ; char input[1000]; int pos,num; set<int> tree[60]; void buildtree(int p){ int k; while(input[pos] != '(') pos ++; pos ++; k = 0; while(input[pos] ==' '){ pos ++; } k *= 10; k += input[pos] -'0'; pos ++; } if(p!=0){ tree[p].insert(k); tree[k].insert(p); } num ++; while(input[pos] !=')'){ while(input[pos] ==' ') pos ++; if(input[pos] =='(') buildtree(k); } pos ++; } void fun(){ int i,k; int print = 0; priority_queue<int, vector<int>, greater<int> > q;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: set&lt;int&gt;::iterator iter; for(i=1;tree[i].size();i++){ if(tree[i].size() ==1){ q.push(i); } } k = num; while(k-- &gt; 1){ i = q.top(); q.pop(); iter = tree[i].begin(); tree[*iter].erase(i); if(tree[*iter].size()==1){ q.push(*iter); } tree[i].clear(); i = *iter; if(print ){ printf(&quot; %d&quot;,i); } else { printf(&quot;%d&quot;, i); print = 1; } } printf(&quot;\n&quot;); } int main(){ int tstcase; int root,i; char c; fgets(input, 1000, stdin); while(!feof(stdin)){ num = pos = 0; buildtree(0); fun(); fgets(input, 1000, stdin); } return 0; }...
View Full Document

Page1 / 2

1097 - set&amp;amp;lt;int&amp;amp;gt;::iterator iter;...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online