Segment Routing over IPv6 is a next-generation IP bearer protocol.
In traditional IP routing, an IP packet sent from a source host goes through multiple routers and then reaches the destination host. Each router checks the destination IP address inside the IP packet header. Then sent the packet to the next router which is closer to the destination. Technically, we call the next closest router “next hop”. Of course, the “next hop” has to be a neighbor of the current router in the graph.
Routers typically maintain a routing table, lists the next hop to particular network destinations. Whenever a new IP packet comes, a router examines the packet header’s destination IP address and compares it against a routing table to determine the packet’s best next hop.
No wonder looking up routing tables takes time, if an IP packet goes through K routers to reach the destination, the looking up process will happen K times at each router. That’s one reason why there is high latency between two distant hosts.
In a segment routed network, an IP packet may be prepended a header that contains a list of “segments”, which are instructions that are executed on subsequent nodes in the network. These instructions may be forwarding instructions, such as an instruction to forward a packet to a specific destination or interface.
We can consider the internet as a directed graph.
For example, consider the following topology.
±+ #2 ±+ #1 ±+
|A±------------>|C±--------->|D|
+++ +++ ±+
| |
| |
| |
| |
| |
| |
#1| |
| |#2
| |
| |
| |
v |
±+ |
|B|<--------------+
±+
Router A has two out-edges, its #1 out edge is connected to Router B and its #2 out edge is connected to Router C.
Router C has two out-edges, too. Its #1 out edge is connected to Router D and its #2 out edge is connected to Router B.
Suppose A is going to send a packet to Router D. The only path in this graph is A->C->D. The link used is the #2 out edge of A, and the #1 out edge of C.
A is going to receive a list of instructions along with the packet if segment routing protocol is enabled:
The first instruction is to forward the package by #2 out-edge of the current vertex (Router A), the second instruction is to forward the package by #1 out-edge of the current vertex (Router C). Hence, the routers will check the instructions in the packet header to decide the next hop. The routing table looking up is no longer needed.
Now we have a network log recording the SRv6 packet headers used during last month. Unfortunately, the destination addresses get lost, can you recover them from the forward instructions?
Input
There are T test cases in this problem.
The first line has one integer T.
For each test case:
The first line has two integers N and M. N denotes the number of vertices in the graph, i.e. the number of routers in the network. Routers are labelled from 1 to N. M is the number of packet records in the log file.
In the next N lines, the i-th line begin with an integer D i , meaning that Router i has D i outgoing edge(s). In the following D i integers, the j-th integer O ij indicates that there is an edge from Router i to Router O ij
, and this edge is labelled as the j-th outgoing edge of Router i.In the next M lines, the i-th line begins with two numbers S i and L i . S iindicates the source router of the i-th packet. L i indicates the total number of edges used for this packet. Followed by L i integers, the j-th integer R ij indicates the j-th router in the forwarding path uses the R ij
-th outgoing edge to forward the packet.
Output
For each test case, you should first print a line "Case #t: " (without quotes, with an ending space), where t is the index of this test case, then output M lines.
If a packet could reach a router followed by the route given by the i-th record in the log, the i-th output line should contain one integer T i , indicating the destination node for the i-th record.
Otherwise, if the route given by the i-th recorder is illegal, output “Packet Loss” (without quotes). For instance, the header may instruct the i-th router to use the 10-th outgoing edge for forwarding while router i only has 8 outgoing edges.
题意:
给定T组询问,每组询问给定一个n和m,接下来n行每行给定一个cnt然后输入cnt个点,表示当前行和输入的u之间有一条有向边,然后接下来的m行每行输入一个now 和 cnt 表示从now这个点开始走cnt次,每一次输入一个ttt,表示从now这个点走的第ttt个边,如果不存在这个出"Packet Loss",否则u就走到这个点,最后输出能到的哪个点。在这个过程中不要碰到到不了的点就退出,要把这组的数据全部输入进来
题解:
开一个二维vector来存当前这个点能到的所有的点即可
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
#define ll long long
vector <int > s[100005];
int main()
{
int t;
cin>>t;
for(int i=1; i<=t; i++)
{
printf("Case #%d: \n",i);
int n,m;
scanf("%d%d",&n,&m);
for(int j=1; j<=n; j++)
{
s[j].clear();
int cnt=0;
scanf("%d",&cnt);
for(int k=1; k<=cnt; k++)
{
int u;
scanf("%d",&u);
s[j].push_back(u);
}
}
for(int j=1; j<=m; j++)
{
int pan=0;
int now,cnt=0;
scanf("%d%d",&now,&cnt);
int vis=now;
int ttt;
for(int k=1; k<=cnt; k++)
{
scanf("%d",&ttt);
if(ttt==0||ttt>s[vis].size())
pan=1;
if(pan)
continue;
vis=s[vis][ttt-1];
}
if(pan!=0)
cout << "Packet Loss" << endl;
else
cout << vis << endl;
}
}
}