/ Vijos / 讨论 / 题解 /

P1112小胖的奇偶

var
n:longint;
m:longint;
i,j,k,t:longint;

b,d,f:array [1..10000] of longint;
line:array [1..5000,1..3] of longint;
max:longint;
total:longint;
c:char;

procedure swap(var a,b:longint);
var
z:longint;
begin
z:=a;a:=b;b:=z;
end;

function find(x:longint):longint;
var
m:longint;
begin
if f[x]=x then exit(x);
m:=f[x];
f[x]:=find(f[x]);
b[x]:=(b[x]+b[m]) mod 2;
exit(f[x]);
end;

procedure sort(i,j:longint);
var
key,x1,x2:longint;
begin
if i<j then
begin
x1:=i;x2:=j;key:=d[(i+j) shr 1];
repeat
while d[x1]<key do inc(x1);
while d[x2]>key do dec(x2);
if x1<=x2 then
begin
swap(d[x1],d[x2]);
inc(x1);dec(X2);
end;
until x1>x2;
if x1<j then sort(x1,j);
if x2>i then sort(i,x2);
end;
end;

function ef(x:longint):longint;
var
l,r,mid:longint;
begin
l:=1; r:=total;
while l<=r do
begin
mid:=(l+r) div 2;
if d[mid]<x then
l:=mid+1
else
if d[mid]>x then r:=mid-1
else
exit(mid);
end;

exit(-1);
end;

procedure union(x,y,p:longint);
var
i,x1,x2:longint;
begin
x1:=find(x);
x2:=find(y);
if (x1<>x2) then
begin
f[x1]:=x2;
i:=b[x]+b[y]+p;
i:=i mod 2;
b[x1]:=i;
end;
end;

procedure init;
var
x,y,l,r:longint;
begin
readln(n);readln(m);
for i:=1 to m do
begin
read(line[i,1],line[i,2]);
read(c);
readln(c);
if c='e' then
line[i,3]:=0
else
line[i,3]:=1;
d[2*i-1]:=line[i,1]-1;
d[2*i]:=line[i,2];
end;

sort(1,2*m);

total:=1;

for i:=2 to 2*m do
begin
if d[i]<>d[total] then
begin
inc(total);
d[total]:=d[i];
end;
end;

for i:=1 to total do
f[i]:=i;

for i:=1 to m do
begin
x:=line[i,1]-1;
y:=line[i,2];
x:=ef(x);
y:=ef(y);
l:=find(x);
r:=find(y);
if l<>r then
union(x,y,line[i,3])
else
begin
x:=b[x];
y:=b[y];
if (y+x+line[i,3]+2) mod 2<>0 then
begin
writeln(i-1);
halt;
end;
end;
end;

writeln(m);
end;

begin
init;
end.

3 条评论

  • @ 2013-10-06 00:14:13

    希望以后题解能发到各题目的题解区下,谢谢合作!
    I wish that the problem solutions can be sent to each topic area under the problem solution, thank you!
    Решение Надежда проблема может быть отправлен в каждой тематической области под решение проблемы, спасибо!
    希望以後題解能發到各題目的題解區下,謝謝合作!
    ホープ問題解決、問題の解決策の下で、各トピックエリアに送信することができ、ありがとうございました!
    La solution du problème de l'espoir peut être envoyé à chaque sujet dans la solution du problème, merci!
    Rozwiązanie problemu Nadzieja mogą być wysyłane do każdego obszaru tematu w ramach rozwiązania problemu, dziękuję!

  • @ 2013-10-05 16:51:30

    ……sorry没解释清楚……这个貌似应该发在题目里面的题解区里,即题目里的查看题解里……

  • @ 2013-10-05 16:13:19

    ###Block Code
    var
    n:longint;
    m:longint;
    i,j,k,t:longint;

    b,d,f:array [1..10000] of longint;
    line:array [1..5000,1..3] of longint;
    max:longint;
    total:longint;
    c:char;

    procedure swap(var a,b:longint);
    var
    z:longint;
    begin
    z:=a;a:=b;b:=z;
    end;

    function find(x:longint):longint;
    var
    m:longint;
    begin
    if f[x]=x then exit(x);
    m:=f[x];
    f[x]:=find(f[x]);
    b[x]:=(b[x]+b[m]) mod 2;
    exit(f[x]);
    end;

    procedure sort(i,j:longint);
    var
    key,x1,x2:longint;
    begin
    if i<j then
    begin
    x1:=i;x2:=j;key:=d[(i+j) shr 1];
    repeat
    while d[x1]<key do inc(x1);
    while d[x2]>key do dec(x2);
    if x1<=x2 then
    begin
    swap(d[x1],d[x2]);
    inc(x1);dec(X2);
    end;
    until x1>x2;
    if x1<j then sort(x1,j);
    if x2>i then sort(i,x2);
    end;
    end;

    function ef(x:longint):longint;
    var
    l,r,mid:longint;
    begin
    l:=1; r:=total;
    while l<=r do
    begin
    mid:=(l+r) div 2;
    if d[mid]<x then
    l:=mid+1
    else
    if d[mid]>x then r:=mid-1
    else
    exit(mid);
    end;

    exit(-1);
    end;

    procedure union(x,y,p:longint);
    var
    i,x1,x2:longint;
    begin
    x1:=find(x);
    x2:=find(y);
    if (x1<>x2) then
    begin
    f[x1]:=x2;
    i:=b[x]+b[y]+p;
    i:=i mod 2;
    b[x1]:=i;
    end;
    end;

    procedure init;
    var
    x,y,l,r:longint;
    begin
    readln(n);readln(m);
    for i:=1 to m do
    begin
    read(line[i,1],line[i,2]);
    read(c);
    readln(c);
    if c='e' then
    line[i,3]:=0
    else
    line[i,3]:=1;
    d[2*i-1]:=line[i,1]-1;
    d[2*i]:=line[i,2];
    end;

    sort(1,2*m);

    total:=1;

    for i:=2 to 2*m do
    begin
    if d[i]<>d[total] then
    begin
    inc(total);
    d[total]:=d[i];
    end;
    end;

    for i:=1 to total do
    f[i]:=i;

    for i:=1 to m do
    begin
    x:=line[i,1]-1;
    y:=line[i,2];
    x:=ef(x);
    y:=ef(y);
    l:=find(x);
    r:=find(y);
    if l<>r then
    union(x,y,line[i,3])
    else
    begin
    x:=b[x];
    y:=b[y];
    if (y+x+line[i,3]+2) mod 2<>0 then
    begin
    writeln(i-1);
    halt;
    end;
    end;
    end;

    writeln(m);
    end;

    begin
    init;
    end.

  • 1