ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1069. Prufer Code

test#4(access violation)
Posted by LNCP 6 Feb 2015 21:33
HELP!My algo:find the minimum,add the edge,sort and print,the first and the third step I use the heap.I don't think these matter.
My code:

#include<iostream>
using namespace std;
const int maxn = 7500;
int heap_size = 0, now = 0;
int heap[maxn + 1];
struct edge
{
    int neighbour;
    edge *pre;
}e[maxn + 1];
edge *tail[maxn + 1];
inline void swap(int i, int j)
{
    int temp;
    temp = heap[i];
    heap[i] = heap[j];
    heap[j] = temp;
    return;
}
void heap_up(int i)
{
    if (i == 1) return;
    int father = i / 2;
    if (heap[i] < heap[father])
    {
        swap(i, father);
        heap_up(father);
    }
    return;
}
void heap_down(int i)
{
    int left = i * 2;
    int right = left + 1;
    int least = i;
    if (left <= heap_size && heap[left] < heap[least]) least = left;
    if (right <= heap_size && heap[right] < heap[least]) least = right;
    if (least != i)
    {
        swap(i, least);
        heap_down(least);
    }
    return;
}
inline void heap_delete(int i)
{
    swap(i, heap_size--);
    heap_down(i);
    return;
}
inline void heap_insert(int key)
{
    heap[++heap_size] = key;
    heap_up(heap_size);
    return;
}
inline void connect(int u, int v)
{
    e[++now].neighbour = v;
    e[now].pre = tail[u];
    tail[u] = e + now;
    return;
}
int main()
{
    int son[maxn + 1] = { 0 }, code[maxn];
    int n = 1, i, u, v, temp;
    do
    {
        temp = 0;
        cin >> temp;
        if (temp)
        {
            code[n++] = temp;
            son[temp]++;
        }
    } while (temp);
    for (i = 1; i <= n; i++) if (!son[i]) heap_insert(i);
    for (i = 1; i <= n; i++) tail[i] = nullptr;
    for (i = 1; i < n; i++)
    {
        v = heap[1];
        heap_delete(1);
        u = code[i];
        if (!(--son[u])) heap_insert(u);
        connect(u, v);
        connect(v, u);
    }
    edge *p;
    heap_size = 0;
    for (i = 1; i <= n; i++)
    {
        cout << i << ":";
        for (p = tail[i]; p != nullptr; p = p->pre) heap_insert(p->neighbour);
        for (; heap_size; heap_delete(1)) cout << " " << heap[1];
        cout << endl;
    }
    return 0;
}

Edited by author 06.02.2015 21:40

Edited by author 07.02.2015 08:44
Re: test#4(access violation)
Posted by lakerka 16 May 2015 05:25
Somewhere there should be bad indexing. Try to increase arrays size from maxn + 1 to maxn + 50 and check what results judge gives.