/**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣤⡤⠤⣤⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡴⠛⠉⠀⠀⠀⠀⠈⠙⢷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⠶⣄⡀⠀⠀⠀⢠⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡏⠀⠀⠉⠛⠶⣤⣸⡇⠀⠀⠀⠀⣀⣤⣶⣶⠒⠒⠒⠶⣬⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣤⠴⠶⣿⠀⠀⠀⠀⠀⠀⠈⠉⠉⠛⠒⠶⢿⣭⣀⡀⢻⡀⠀⠀⢠⡿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⠶⠛⠋⠁⠀⠀⢸⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠛⠷⣴⣞⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⡴⠞⠉⠀⠀⠀⠀⠀⠀⢰⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⢦⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠞⠋⠀⠀⠀⠀⢀⡤⠠⡄⠀⢰⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠿⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡞⠁⠀⠀⠀⠀⣠⠖⠋⠀⣸⠇⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⢀⡀⠀⠀⠀⠀⠀⠀⢦⡀⠀⠀⠀⠸⣷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡼⠋⠀⠀⠀⠀⢀⣴⠋⢀⢀⡴⠋⠀⢀⣼⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣽⠀⣼⢿⡄⠀⠀⠀⠀⣆⠀⠉⢦⡀⠀⠀⠀⠘⢧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡟⠁⠀⠀⠀⠀⣠⠟⠇⠀⠈⠉⠁⠀⣀⣾⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡟⢰⠏⠀⠻⣄⠀⠀⠀⠹⣄⣰⠟⠁⠀⠀⠀⠀⠀⢻⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⡟⠀⢀⣀⡖⠀⣰⠏⠀⠀⠀⠀⠀⢀⣼⣿⠋⠸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣧⡟⠀⠀⠀⠹⣦⠀⠀⠀⠀⠁⠀⣶⠀⠀⣴⠛⢧⠀⢻⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣠⠞⣩⡟⠀⠀⠈⠉⠀⢀⡟⠀⠀⢀⣀⣠⣤⣾⡿⠗⠒⠚⣿⠠⣤⠀⠀⠀⠀⠀⠀⠀⢸⣿⠓⠒⠲⠶⠶⠾⢷⣤⣀⣀⠀⠀⠙⣧⠀⠹⣆⣼⠃⠀⢷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⡴⠟⢁⡼⣿⠁⠀⠀⠀⠀⠀⢸⠃⠀⠀⠈⠉⢠⡾⠋⠀⠀⠀⠀⠸⣆⠙⣧⡀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠻⣆⠉⠁⠀⠀⢹⡄⠀⠈⠁⠀⠀⠘⣷⢦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⣠⡶⠋⢀⡴⠋⢰⡏⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⣠⠟⠀⠀⠀⠀⠀⠀⠀⢻⡄⢸⣷⣄⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠈⢳⣄⠀⠀⠸⣧⠀⠀⠀⠀⠀⠀⢿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⢰⣾⡥⠴⠞⠋⠀⠀⣼⠀⠀⠀⠀⠀⠀⠀⣸⠀⠀⠀⣰⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⡄⣷⠙⠷⣄⡀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢷⣄⠀⣿⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣿⡆⠀⠀⠀⠀⠀⠀⢸⡄⠀⣼⢏⣀⣤⣶⣦⣤⣶⣶⣶⣶⣶⣿⣿⣾⡆⠀⠈⠻⢦⣼⡇⢰⣶⣶⣶⣶⣶⣶⣶⣤⣤⣤⣦⠙⢧⣿⠀⠀⠀⠀⠀⠀⢸⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣿⡇⠀⠀⠀⠀⠀⠀⠘⣇⣼⠏⠘⣿⡿⢿⣿⣿⣿⣿⣿⡏⠉⠉⠉⠙⠃⠀⠀⠀⠀⠉⠁⠘⠛⢻⣿⣿⣿⣿⣿⣟⠛⢛⠷⠀⠀⣿⠀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⡟⣷⠀⠀⠀⠀⠀⠀⠀⢻⡏⠀⠀⠀⠀⣸⣿⣿⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣾⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⢰⡏⠀⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣷⢹⣆⠀⠀⠀⠀⠀⠀⠈⣷⠀⠀⠀⠀⢹⣿⣿⣿⣿⣿⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⣾⠀⠀⠀⠀⠀⠀⢰⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢿⠘⣿⣆⠀⠀⠀⠀⠀⠐⠘⣧⠀⠀⠀⠘⢿⣿⣿⣿⣿⠏⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿⣿⡿⠃⠀⠀⠀⣼⠃⠀⠀⠀⠀⠀⣰⠟⠐⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠸⣇⣿⠙⣧⣄⠀⠀⠀⠀⠀⠘⢧⡀⠀⠀⠈⢹⠿⢟⡋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢻⡿⠀⡀⠀⠀⣼⠃⠀⠀⠀⠀⣀⡾⠋⠀⣴⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⣿⠀⣿⠝⠳⣤⣀⡀⠀⠀⠈⢷⣤⠇⢠⡞⠠⠾⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠟⠁⡾⠃⣠⡾⠃⠀⠀⣀⣤⠾⠋⠀⠀⠀⡿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣸⣿⠀⠀⠀⠉⠙⠓⡶⠦⠤⣿⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠓⢰⡿⠤⠴⠶⣿⠉⠀⠀⠀⠀⠀⢠⡇⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⢻⠀⠀⠀⠀⠀⠃⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢲⣄⠀⠀⠀⠀⠀⠀⠀⢀⣴⠆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡟⠀⠀⠀⠀⠀⠀⢸⡇⣸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡿⢸⡇⠀⠀⠀⠀⠀⢿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠛⠲⠤⣤⠤⠴⠞⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡏⠀⠀⠀⠀⠀⠀⢸⠇⠈⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⡇⢸⡇⠀⠀⠀⠀⠀⢸⣿⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢿⡇⠀⠀⠀⠀⠀⠀⢺⢀⠀⢿⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⢷⠛⣇⠀⠀⠀⠀⠀⠈⣿⠉⠻⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⣅⣸⠀⠀⠀⠀⠀⠀⢀⣿⢸⡀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡿⠘⣀⣿⠀⠀⠀⠀⠀⢷⢸⡄⠀⠈⠙⠶⣤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⡴⠟⠁⠀⠈⣿⠀⠀⠀⠀⠀⠀⠈⡟⣼⡇⠀⣧⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡇⠀⢿⢿⠀⠀⠀⠀⠀⠀⠈⣧⠀⠀⠀⠀⠀⠉⠛⠶⢤⣤⣀⣀⠀⣀⡀⠀⠀⠀⠀⠀⢀⣠⡴⠞⠋⠁⠀⠀⠀⠀⢀⡏⠀⠀⠀⠀⠀⠀⢠⡇⢹⣤⠀⢹⡄⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⠀⠀⡿⢸⡆⠀⠀⠀⠀⠀⠀⢻⡆⠀⠀⠀⠀⠀⠀⠀⠀⣿⡿⠟⠋⠉⠁⠀⠀⠀⠀⠀⢸⠁⠀⠀⠀⠀⠀⠀⠀⠀⢸⠇⠀⠀⠀⠀⠀⠀⢸⡇⠈⣟⠀⠈⣷⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢰⡟⠀⢰⡇⠘⡇⠀⠀⠀⠀⠀⠘⠀⣷⠀⠀⠀⠀⠀⠀⠀⠀⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⡿⠆⠀⠀⠀⠀⠀⠀⣼⠁⠀⢻⡀⠀⠸⣇⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣼⠁⠀⣼⠁⠀⣿⠀⠀⠀⠀⠀⠀⠀⠻⣇⣀⠀⠀⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡀⠀⠀⠀⠀⠀⠀⠀⣰⡇⠀⠀⠀⠀⠀⠀⠀⣿⡆⠀⠈⣷⡀⠀⢻⡄⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢰⡇⠀⢀⡏⠀⠀⢻⠀⠀⠀⠀⠀⠀⠀⠀⢻⣦⣄⠀⠀⠀⠀⣠⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣧⠀⠀⠀⠀⠀⣠⣾⣿⠀⠀⠀⠀⠀⠀⢠⣠⡇⡇⠀⠀⠸⣯⡀⠀⢷⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⡾⠀⠀⣼⠁⠀⠀⠸⡇⠀⠀⠀⠀⠀⠀⠀⠀⢿⣽⡧⠴⢶⣿⣿⠖⠒⠛⠛⠃⠀⠀⠀⠚⠋⠉⠉⠉⠙⠓⠲⢾⡛⠻⣽⠃⠀⠀⠀⠀⠀⠀⢸⣿⣃⠀⠀⠀⠀⢹⣷⠀⠘⣧⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⣸⠃⠀⢸⡏⠀⠀⠀⢀⣿⠀⠀⠀⠀⢀⣀⠀⠈⠈⢷⠖⠚⠋⠁⢹⡇⠀⢀⣴⠶⠶⢦⣄⣀⣤⡶⠶⠤⠤⠤⠶⠾⠇⢰⡏⠀⠀⠀⠀⠀⠀⠻⣿⣿⣤⡀⠀⠀⠀⠀⢿⣀⠀⠸⣆⠀⠀⠀
⠀⠀⠀⠀⠀⢰⠏⠀⢀⡿⠀⠀⠀⣰⠟⢹⣿⡄⠀⠀⠀⠻⣄⠀⠀⠘⣷⣤⣄⣀⣈⡙⠛⢹⡷⢶⣦⣴⣾⣛⣻⢯⣴⣦⠴⠖⠃⠀⢀⡾⠀⠀⠀⠀⠀⠀⠀⢰⣿⡇⠀⠹⣆⠀⠀⠸⡞⣧⣆⠀⠹⣄⠀⠀
⠀⠀⠀⠀⢠⡟⠀⠀⡼⠁⠀⠀⣰⠏⠀⠈⣟⣧⠀⠀⠀⠀⢻⣆⠀⠀⠈⣧⡿⠀⠈⠉⠛⠛⣻⣿⡿⢿⣿⡍⠉⠀⠀⠀⠀⠀⠀⠀⡾⠁⠀⠀⠀⢀⣴⠀⠀⡾⣿⠁⠀⠀⠘⣧⠀⠀⢿⠘⣟⠀⠀⢻⡄⠀
⠀⠀⢀⣠⡟⠀⢀⡾⠃⠀⠀⣰⡏⠀⠀⠀⣻⠘⣧⠀⠀⠀⠀⢻⣷⡄⠀⠘⢷⡀⠀⠀⠀⠀⠩⣉⠁⠈⣛⡁⠀⠀⠀⠀⠀⠀⣀⣾⠁⠀⠀⣀⣠⣟⠁⠀⣠⣤⡟⠀⠀⠀⠀⠘⣧⡄⠘⡇⠙⣇⠀⠀⠻
*/
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define i64 long long
#define ld long double
#define bit(n,i) ((n>>i)&1)
#define pii pair<int,int>
#define sz(x) (int)x.size()
#define FOR(i,a,b) for(int i=a; i<=b; i++)
#define FOD(i,a,b) for(int i=a; i>=b; i--)
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(),x.end()
#define __sumairu__ signed main()
#define die_hard_onimai_fan void seggs()
#define file(name) if(fopen(name".inp","r")){freopen(name".inp","r",stdin);freopen(name".out","w",stdout);}
#define brute(name) if(fopen(name".inp","r")){freopen(name".inp","r",stdin);freopen(name".ans","w",stdout);}
#define TIME (1.0*clock()/CLOCKS_PER_SEC)
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ai(n) array<int,n>
#define dbg(x) {cerr<<#x<<' '<<x<<endl;}
#define dbgArr(arr,n) {cerr<<#arr;FOR(_i,1,n)cerr<<' '<<(arr)[_i];cerr<<endl;}
template <typename T,typename U>
ostream& operator<<(ostream &os,pair<T,U>p){
return os<<"{"<<p.fi<<", "<<p.se<<"}";
}
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
i64 Rand(i64 l,i64 r)
{
i64 ans=l+1ll*rd()*rd()%(r-l+1);
assert(l<=ans&&ans<=r);
return ans;
}
//const i64 base=1e9+7;
//const i64 mod=(1ll<<53)+5;
#define i64 long long
#define debug 0
const int mod=1e9+7;
//const int mod=998244353;
const int inf=1e9;
///check the limits, dummy
const int N=3e5+5;
struct Fenwick
{
int bit[N];
int get(int i)
{
int ans=0;
for(++i;i>0;i-=i&-i)ans+=bit[i];
return ans;
}
int get(int l,int r)
{
return get(r)-get(l-1);
}
void add(int i,int v)
{
for(++i;i<N;i+=i&-i)bit[i]+=v;
}
}f;
vector<pii>adj[N],fromto[N];
vector<int>cnt[N];
int n,q;
int ans[N],sz[N];
bool vis[N];
int dfsz(int u,int p=0)
{
sz[u]=1;
for(auto&&[id,v]:adj[u])if(!vis[v]&&v!=p)sz[u]+=dfsz(v,u);
return sz[u];
}
int centroid(int u,int S,int p=0)
{
for(auto&&[id,v]:adj[u])if(!vis[v]&&v!=p&&sz[v]>S/2)return centroid(v,S,u);
return u;
}
int in[N];
void upd(int u)
{
// cout<<"! "<<u<<'\n';
for(auto&&[id,v]:fromto[u])
{
if(u==v||in[v]&&in[v]<=id)ans[id]=-1;
// cout<<"fromto: "<<id<<' '<<v<<' '<<in[v]<<'\n';
}
for(int id:cnt[u])
{
ans[id]+=f.get(id);
// cout<<"cnt: "<<id<<'\n';
}
}
/// take
void dfs(int u,int p,int cur)
{
upd(u);
for(auto&&[id,v]:adj[u])if(!vis[v]&&v!=p&&id<=cur)
{
dfs(v,u,id);
}
}
/// add
void dfs2(int u,int p,int cur)
{
in[u]=cur;
f.add(cur,1);
for(auto&&[id,v]:adj[u])if(!vis[v]&&v!=p&&cur<=id)
{
dfs2(v,u,id);
}
}
/// remove
void dfs3(int u,int p,int cur)
{
in[u]=0;
f.add(cur,-1);
for(auto&&[id,v]:adj[u])if(!vis[v]&&v!=p&&cur<=id)
{
dfs3(v,u,id);
}
}
void solve(int u)
{
// cout<<u<<' ';
u=centroid(u,dfsz(u));
// cout<<u<<'\n';
vis[u]=1;
for(auto&&[id,v]:adj[u])if(!vis[v])
{
// dbgArr(in,n);
in[u]=id;
f.add(id,1);
dfs(v,u,id);
f.add(id,-1);
dfs2(v,u,id);
}
upd(u);
in[u]=0;
for(auto&&[id,v]:adj[u])if(!vis[v])
{
dfs3(v,u,id);
}
assert(f.get(N-1)==0);
for(auto&&[id,v]:adj[u])if(!vis[v])solve(v);
}
die_hard_onimai_fan
{
cin>>n>>q;
FOR(i,1,n+q-1)
{
char o;
cin>>o;
if(o=='S')
{
int u,v;
cin>>u>>v;
adj[u].pb({i,v});
adj[v].pb({i,u});
ans[i]=-3;
}
else if(o=='Q')
{
int u,v;
cin>>u>>v;
fromto[v].pb({i,u});
ans[i]=-2;
}
else
{
int x;
cin>>x;
cnt[x].pb(i);
}
}
FOR(i,1,n)reverse(all(adj[i]));
solve(1);
FOR(i,1,n+q-1)if(ans[i]!=-3)
{
if(ans[i]>=0)cout<<ans[i]+1<<'\n';
else cout<<(ans[i]==-1?"yes\n":"no\n");
}
}
__sumairu__
{
FAST
file("");
int tt=1;//cin>>tt;
while(tt--)seggs();
cerr<<"\nTime elapsed: "<<TIME<<" s.\n";
}
/**
6 9
S 1 2
S 1 3
S 3 4
Q 5 1
S 4 5
S 1 6
Q 5 1
Q 1 5
C 1
C 2
C 3
C 4
C 5
C 6
4 4
S 1 2
S 1 3
S 3 4
Q 2 1
Q 2 2
Q 2 3
Q 2 4
*/
Compilation message
servers.cpp: In function 'void upd(int)':
servers.cpp:137:23: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
137 | if(u==v||in[v]&&in[v]<=id)ans[id]=-1;
| ~~~~~^~~~~~~~~~~
servers.cpp: In function 'int main()':
servers.cpp:61:53: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | #define file(name) if(fopen(name".inp","r")){freopen(name".inp","r",stdin);freopen(name".out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
servers.cpp:251:5: note: in expansion of macro 'file'
251 | file("");
| ^~~~
servers.cpp:61:83: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | #define file(name) if(fopen(name".inp","r")){freopen(name".inp","r",stdin);freopen(name".out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
servers.cpp:251:5: note: in expansion of macro 'file'
251 | file("");
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
25436 KB |
Output is correct |
2 |
Correct |
31 ms |
26200 KB |
Output is correct |
3 |
Correct |
29 ms |
25940 KB |
Output is correct |
4 |
Correct |
33 ms |
26236 KB |
Output is correct |
5 |
Correct |
31 ms |
25940 KB |
Output is correct |
6 |
Correct |
33 ms |
25672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
25436 KB |
Output is correct |
2 |
Correct |
31 ms |
26200 KB |
Output is correct |
3 |
Correct |
29 ms |
25940 KB |
Output is correct |
4 |
Correct |
33 ms |
26236 KB |
Output is correct |
5 |
Correct |
31 ms |
25940 KB |
Output is correct |
6 |
Correct |
33 ms |
25672 KB |
Output is correct |
7 |
Correct |
25 ms |
25172 KB |
Output is correct |
8 |
Correct |
47 ms |
25420 KB |
Output is correct |
9 |
Correct |
31 ms |
25172 KB |
Output is correct |
10 |
Correct |
38 ms |
25424 KB |
Output is correct |
11 |
Correct |
35 ms |
25184 KB |
Output is correct |
12 |
Correct |
32 ms |
25172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
25436 KB |
Output is correct |
2 |
Correct |
110 ms |
34772 KB |
Output is correct |
3 |
Correct |
99 ms |
34756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
25436 KB |
Output is correct |
2 |
Correct |
110 ms |
34772 KB |
Output is correct |
3 |
Correct |
99 ms |
34756 KB |
Output is correct |
4 |
Correct |
28 ms |
25168 KB |
Output is correct |
5 |
Correct |
97 ms |
34248 KB |
Output is correct |
6 |
Correct |
67 ms |
31936 KB |
Output is correct |
7 |
Correct |
71 ms |
32192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
25180 KB |
Output is correct |
2 |
Correct |
162 ms |
41960 KB |
Output is correct |
3 |
Correct |
144 ms |
41808 KB |
Output is correct |
4 |
Correct |
147 ms |
41556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
25180 KB |
Output is correct |
2 |
Correct |
162 ms |
41960 KB |
Output is correct |
3 |
Correct |
144 ms |
41808 KB |
Output is correct |
4 |
Correct |
147 ms |
41556 KB |
Output is correct |
5 |
Correct |
25 ms |
25176 KB |
Output is correct |
6 |
Correct |
185 ms |
41812 KB |
Output is correct |
7 |
Correct |
164 ms |
41904 KB |
Output is correct |
8 |
Correct |
156 ms |
41040 KB |
Output is correct |
9 |
Correct |
155 ms |
41036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
25180 KB |
Output is correct |
2 |
Correct |
127 ms |
35080 KB |
Output is correct |
3 |
Correct |
125 ms |
35348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
25180 KB |
Output is correct |
2 |
Correct |
127 ms |
35080 KB |
Output is correct |
3 |
Correct |
125 ms |
35348 KB |
Output is correct |
4 |
Correct |
22 ms |
25084 KB |
Output is correct |
5 |
Correct |
140 ms |
35168 KB |
Output is correct |
6 |
Correct |
154 ms |
35164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
25316 KB |
Output is correct |
2 |
Correct |
146 ms |
41812 KB |
Output is correct |
3 |
Correct |
148 ms |
41836 KB |
Output is correct |
4 |
Correct |
128 ms |
41508 KB |
Output is correct |
5 |
Correct |
25 ms |
25176 KB |
Output is correct |
6 |
Correct |
131 ms |
35056 KB |
Output is correct |
7 |
Correct |
131 ms |
35412 KB |
Output is correct |
8 |
Correct |
138 ms |
35584 KB |
Output is correct |
9 |
Correct |
127 ms |
35920 KB |
Output is correct |
10 |
Correct |
196 ms |
38996 KB |
Output is correct |
11 |
Correct |
225 ms |
37424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
25316 KB |
Output is correct |
2 |
Correct |
146 ms |
41812 KB |
Output is correct |
3 |
Correct |
148 ms |
41836 KB |
Output is correct |
4 |
Correct |
128 ms |
41508 KB |
Output is correct |
5 |
Correct |
25 ms |
25176 KB |
Output is correct |
6 |
Correct |
131 ms |
35056 KB |
Output is correct |
7 |
Correct |
131 ms |
35412 KB |
Output is correct |
8 |
Correct |
138 ms |
35584 KB |
Output is correct |
9 |
Correct |
127 ms |
35920 KB |
Output is correct |
10 |
Correct |
196 ms |
38996 KB |
Output is correct |
11 |
Correct |
225 ms |
37424 KB |
Output is correct |
12 |
Correct |
29 ms |
25180 KB |
Output is correct |
13 |
Correct |
166 ms |
41812 KB |
Output is correct |
14 |
Correct |
173 ms |
42064 KB |
Output is correct |
15 |
Correct |
166 ms |
41028 KB |
Output is correct |
16 |
Correct |
155 ms |
40936 KB |
Output is correct |
17 |
Correct |
24 ms |
25168 KB |
Output is correct |
18 |
Correct |
147 ms |
35152 KB |
Output is correct |
19 |
Correct |
131 ms |
35152 KB |
Output is correct |
20 |
Correct |
141 ms |
35656 KB |
Output is correct |
21 |
Correct |
150 ms |
36096 KB |
Output is correct |
22 |
Correct |
230 ms |
37456 KB |
Output is correct |
23 |
Correct |
297 ms |
38480 KB |
Output is correct |
24 |
Correct |
234 ms |
38228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
25180 KB |
Output is correct |
2 |
Correct |
40 ms |
25940 KB |
Output is correct |
3 |
Correct |
31 ms |
25912 KB |
Output is correct |
4 |
Correct |
36 ms |
26200 KB |
Output is correct |
5 |
Correct |
31 ms |
25936 KB |
Output is correct |
6 |
Correct |
31 ms |
25680 KB |
Output is correct |
7 |
Correct |
32 ms |
25436 KB |
Output is correct |
8 |
Correct |
84 ms |
34736 KB |
Output is correct |
9 |
Correct |
84 ms |
34752 KB |
Output is correct |
10 |
Correct |
25 ms |
25272 KB |
Output is correct |
11 |
Correct |
154 ms |
41808 KB |
Output is correct |
12 |
Correct |
158 ms |
41924 KB |
Output is correct |
13 |
Correct |
137 ms |
41496 KB |
Output is correct |
14 |
Correct |
24 ms |
25172 KB |
Output is correct |
15 |
Correct |
130 ms |
35152 KB |
Output is correct |
16 |
Correct |
125 ms |
35412 KB |
Output is correct |
17 |
Correct |
125 ms |
35664 KB |
Output is correct |
18 |
Correct |
136 ms |
35940 KB |
Output is correct |
19 |
Correct |
184 ms |
38992 KB |
Output is correct |
20 |
Correct |
181 ms |
37468 KB |
Output is correct |
21 |
Correct |
124 ms |
35160 KB |
Output is correct |
22 |
Correct |
113 ms |
35256 KB |
Output is correct |
23 |
Correct |
111 ms |
35412 KB |
Output is correct |
24 |
Correct |
112 ms |
35668 KB |
Output is correct |
25 |
Correct |
171 ms |
38604 KB |
Output is correct |
26 |
Correct |
141 ms |
34520 KB |
Output is correct |
27 |
Correct |
129 ms |
34388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
25180 KB |
Output is correct |
2 |
Correct |
40 ms |
25940 KB |
Output is correct |
3 |
Correct |
31 ms |
25912 KB |
Output is correct |
4 |
Correct |
36 ms |
26200 KB |
Output is correct |
5 |
Correct |
31 ms |
25936 KB |
Output is correct |
6 |
Correct |
31 ms |
25680 KB |
Output is correct |
7 |
Correct |
32 ms |
25436 KB |
Output is correct |
8 |
Correct |
84 ms |
34736 KB |
Output is correct |
9 |
Correct |
84 ms |
34752 KB |
Output is correct |
10 |
Correct |
25 ms |
25272 KB |
Output is correct |
11 |
Correct |
154 ms |
41808 KB |
Output is correct |
12 |
Correct |
158 ms |
41924 KB |
Output is correct |
13 |
Correct |
137 ms |
41496 KB |
Output is correct |
14 |
Correct |
24 ms |
25172 KB |
Output is correct |
15 |
Correct |
130 ms |
35152 KB |
Output is correct |
16 |
Correct |
125 ms |
35412 KB |
Output is correct |
17 |
Correct |
125 ms |
35664 KB |
Output is correct |
18 |
Correct |
136 ms |
35940 KB |
Output is correct |
19 |
Correct |
184 ms |
38992 KB |
Output is correct |
20 |
Correct |
181 ms |
37468 KB |
Output is correct |
21 |
Correct |
124 ms |
35160 KB |
Output is correct |
22 |
Correct |
113 ms |
35256 KB |
Output is correct |
23 |
Correct |
111 ms |
35412 KB |
Output is correct |
24 |
Correct |
112 ms |
35668 KB |
Output is correct |
25 |
Correct |
171 ms |
38604 KB |
Output is correct |
26 |
Correct |
141 ms |
34520 KB |
Output is correct |
27 |
Correct |
129 ms |
34388 KB |
Output is correct |
28 |
Correct |
27 ms |
25276 KB |
Output is correct |
29 |
Correct |
45 ms |
25508 KB |
Output is correct |
30 |
Correct |
42 ms |
25236 KB |
Output is correct |
31 |
Correct |
37 ms |
25424 KB |
Output is correct |
32 |
Correct |
33 ms |
25172 KB |
Output is correct |
33 |
Correct |
33 ms |
24976 KB |
Output is correct |
34 |
Correct |
26 ms |
25176 KB |
Output is correct |
35 |
Correct |
81 ms |
34388 KB |
Output is correct |
36 |
Correct |
64 ms |
31940 KB |
Output is correct |
37 |
Correct |
68 ms |
32196 KB |
Output is correct |
38 |
Correct |
23 ms |
25180 KB |
Output is correct |
39 |
Correct |
158 ms |
41808 KB |
Output is correct |
40 |
Correct |
153 ms |
42068 KB |
Output is correct |
41 |
Correct |
175 ms |
41044 KB |
Output is correct |
42 |
Correct |
162 ms |
41040 KB |
Output is correct |
43 |
Correct |
26 ms |
25176 KB |
Output is correct |
44 |
Correct |
149 ms |
35432 KB |
Output is correct |
45 |
Correct |
135 ms |
35152 KB |
Output is correct |
46 |
Correct |
128 ms |
35636 KB |
Output is correct |
47 |
Correct |
123 ms |
36180 KB |
Output is correct |
48 |
Correct |
248 ms |
37556 KB |
Output is correct |
49 |
Correct |
229 ms |
38480 KB |
Output is correct |
50 |
Correct |
225 ms |
38224 KB |
Output is correct |
51 |
Correct |
70 ms |
33344 KB |
Output is correct |
52 |
Correct |
70 ms |
32960 KB |
Output is correct |
53 |
Correct |
68 ms |
32496 KB |
Output is correct |
54 |
Correct |
82 ms |
33212 KB |
Output is correct |
55 |
Correct |
72 ms |
32948 KB |
Output is correct |
56 |
Correct |
99 ms |
34640 KB |
Output is correct |
57 |
Correct |
129 ms |
36300 KB |
Output is correct |
58 |
Correct |
155 ms |
34644 KB |
Output is correct |
59 |
Correct |
125 ms |
34384 KB |
Output is correct |