1 // SEGMENT TREES! :D...
19 #define forn(i, n) for(int i=0;i<int(n);i++)
20 #define FOR(i, a, b) for(int i=(a);i<int(b);i++)
21 #define RFOR(i, b, a) for(int i=(b);i>int(a);i--)
22 #define foreach(it, c) for(__typeof((c).begin()) it = (c).begin();it!=(c).end();++it)
23 #define ALL(x) (x).begin(),(x).end()
24 #define SIZE(x) (int)(x).size()
25 #define SORT(x) sort(ALL(x))
27 #define VI vector<int>
28 #define VS vector<string>
30 #define ISS istringstream
31 #define OSS ostringstream
34 vector
<int> edges
[MAXN
];
37 int first
, val
, begin
, end
;
42 void create(int node
, int begin
, int end
){
43 vec
[node
].begin
= begin
; vec
[node
].end
= end
;
44 vec
[node
].first
= -1, vec
[node
].val
= 0;
45 if( begin
== end
)return;
46 create(2*node
,begin
,(end
+begin
)/2);
47 create(2*node
+1,(end
+begin
)/2+1,end
);
52 while( pot
< x
) pot
*=2;
58 void change(int node
, int pos
){
59 if( vec
[node
].begin
!= vec
[node
].end
){
60 if( pos
<= (vec
[node
].begin
+vec
[node
].end
)/2 )change(2*node
,pos
);
61 else change(2*node
+1,pos
);
62 if( vec
[2*node
].first
!=-1 ) vec
[node
].first
= vec
[2*node
].first
;
63 else if( vec
[2*node
+1].first
!= -1 ) vec
[node
].first
= vec
[2*node
+1].first
;
64 else vec
[node
].first
= -1;
67 assert( vec
[node
].begin
== pos
);
68 vec
[node
].val
= 1 - vec
[node
].val
;
69 if( vec
[node
].val
) vec
[node
].first
= vec
[node
].begin
;
70 else vec
[node
].first
= -1;
73 bool inrange(int pos
, int node
){
74 if( pos
>= vec
[node
].begin
&& pos
<= vec
[node
].end
) return true;
77 int query(int node
, int end
){
78 if( vec
[node
].end
<= end
)return vec
[node
].first
;
79 if( vec
[node
].begin
> end
)return -1;
80 if( vec
[node
].begin
== vec
[node
].end
) return -1;
82 if( inrange(end
,2*node
)){a
= query(2*node
,end
);if(a
>=0) return a
;}
83 if( inrange(end
,2*node
+1)){a
= query(2*node
+1,end
); if( a
>=0) return a
;}
88 vector
<STREE
> streeVec
;
89 vector
<vector
<int> > nodosVec
;
90 int depth
[MAXN
],chain
[MAXN
], prev
[MAXN
];
91 int prevStree
[MAXN
], homeStree
[MAXN
], homeIndex
[MAXN
];
92 int dep(int node
){if( node
< 0 ) return -1; return depth
[node
];}
95 void constructStree(){
96 if(nodos
.size() == 0 ) return;
98 for(i
=0;i
<nodos
.size();i
++){
99 prevStree
[nodos
[i
]] = prev
[nodos
[0]];
100 homeStree
[nodos
[i
]] = ind
;
101 homeIndex
[nodos
[i
]] = i
;
104 streeVec
.PB(STREE(nodos
.size()));
107 void go(int node
, int father
){
108 nodos
.push_back(node
);
109 if( edges
[node
].size() == 1 && edges
[node
][0] == father
) constructStree();
112 for(i
=0;i
<edges
[node
].size();i
++){
113 if( edges
[node
][i
] == father
) continue;
114 if( dep(edges
[node
][i
]) > dep(larger
) ) larger
= edges
[node
][i
];
116 if( larger
>= 0 )go(larger
,node
);
117 for(i
=0;i
<edges
[node
].size();i
++){
118 if( edges
[node
][i
] == father
) continue;
119 if( edges
[node
][i
] != larger
){
120 nodos
.clear(); go(edges
[node
][i
],node
);
124 void dfs(int node
, int father
){
127 for(i
=0;i
<edges
[node
].size();i
++){
128 if( edges
[node
][i
] == father
) continue;
129 dfs(edges
[node
][i
], node
);
132 for(i
=0;i
<edges
[node
].size();i
++){
133 if( edges
[node
][i
] == father
) continue;
134 depth
[node
] = max( depth
[node
], depth
[edges
[node
][i
]]+1);
137 void query(int node
){
138 int stree
= homeStree
[node
];
139 int f
= streeVec
[stree
].query( 1, homeIndex
[node
] );
141 if( f
>= 0 ) nod
= nodosVec
[stree
][f
];
142 int last
= prevStree
[node
];
144 stree
= homeStree
[last
];
145 int aux
= streeVec
[stree
].query( 1, homeIndex
[last
] );
148 nod
= nodosVec
[stree
][f
];
150 last
= prevStree
[last
];
152 if(nod
== -1 ) nod
--;
153 printf("%i\n", nod
+1);
155 void change(int node
){
156 int stree
= homeStree
[node
];
157 streeVec
[stree
].change( 1, homeIndex
[node
]);
161 memset(depth
, -1, sizeof(depth
));
162 scanf("%i %i", &N
, &Q
);
165 scanf("%i %i", &a
, &b
);a
--, b
--;
166 edges
[a
].PB(b
), edges
[b
].PB(a
);
173 scanf("%i %i", &op
, &node
);
175 if( op
) query(node
);