【枚举染色】嗅探器 sniffer.pas/c/cpp/in/out

嗅探器

(sniffer.pas/c/cpp/in/out)

Problem

某军搞信息对抗实战演习.红军成功地侵入了蓝军的内部网络.蓝军共有两个信息中心.红军计划在某台中间服务器上安装一个嗅探器,从而能够侦听到两个信息中心互相交换的所有信息.但是蓝军的网络相当的庞大,数据包从一个信息中心传到另一个信息中心可以不止有一条通路.现在需要你尽快地解决这个问题.应该把嗅探器安装在哪个中间服务器上才能保证所有的数据包都能被捕获?

Input

第一行一个整数n(1<=n<=100),表示蓝军网络中服务器的数目.

接下来若干行是对蓝军网络的拓扑结构描述.

每行是两个整数i,j表示编号为I和编号为j的两台服务器间存在连接(显然连接是双向的).服务器的编号从1开始.描述以两个0结束.

再接下来一行是两个整数a,b分别表示两个中心服务器的编号.

Output

输出编号。如果有多个解输出编号最小的一个.如果找不到任何解,输出”No solution”.

Sample Input

5

2 1

2 5

1 4

5 3

2 3

5 1

0 0

4 2

 

Sample Output

 

1

 

 

首先理解一下题目。。(当时就没把题目理解清楚)

红军需要在蓝军的网络里的某一台服务器(中心服务器除外)安装嗅探器,也就是如果在1号点装了,那么所有跟1号点相连的就断了,然后从某一中心服务器开始染色,完了后判断另一中心服务器是否染色,如果染了色仍然代表可以连通,如果没有被染色就代表红军完成了任务。至于输出小的编号,从小到大枚举,找到就停止即可

C++ Code

/*
C++ Code
http://oijzh.cnblogs.com
*/
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 110

int n,a,b;
struct link{int y;link* next;};
link *head[MAXN];
bool h[MAXN];
int num;

void inlink(int x,int y)
{
    link *node=new link;
    node->y=y;
    node->next=head[x];
    head[x]=node;
}

void dfs(int x,int t)
{
    h[x]=true;
    link *node=head[x];
    while(node)
    {
        if(node->y!=t && !h[node->y]) dfs(node->y,t);
        node=node->next;
    }
}

int main()
{
    freopen("sniffer.in","r",stdin);
    freopen("sniffer.out","w",stdout);
    scanf("%d",&n);
    int i,x,y;
    while(1)
    {
        scanf("%d%d",&x,&y);
        if(x==0&&y==0)break;
        inlink(x,y);
        inlink(y,x);
    }
    scanf("%d%d",&a,&b);
    bool flag=false;
    for(i=1;i<=n;i++)
    {
        if(i==a||i==b)continue;
        memset(h,0,sizeof(h));
        dfs(a,i);
        if(!h[b]) {flag=true;break;}
    }
    if(flag)printf("%d",i);
    else printf("No solution");
    return 0;
}

  

 

转载于:https://www.cnblogs.com/oijzh/archive/2012/10/24/2737088.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
—————————————————————— Hearen's SimpleSniffer —————————————————————— 一、功能简介: 1.针对同一局域网中的所有主机进行监听并返回处理优化后的数据供研究使用; 2.在数据表中显示了所有当前侦听到的数据包包括源IP、源端口、目的IP、目的端口、数据包协议类型、数据包捕获时间及数据包简略信息(仅应用层数据); 3.可以针对某一特定IP地址(源或目的),某特定端口(源或目的)以及特定类型数据包进行侦听 -- 捕获前过滤; 4.当数据过多时可以随时点击‘清理’对当前的数据表进行清空 -- 不过捕获的数据是不会被清除的,仅清除列表中显示的数据; 5.双击‘清理’时清空所有到的数据 -- 不仅仅是列表中显示的数据; 6.选择列表中的数据时,数据详细信息会显示在下方的面板中; 此时可以通过选择特定字符串来查看在其左侧的十六进制表示以供研究之用; 7.左下角会显示当前在该局域网中捕获到的数据包个数及总大小(该大小包含IP协议及其建立在该协议以上协议的头部)-- 数据单位会自动进行切换当数据大小达到2G时将重置清零; 8.在获取数据包后可以针对某一IP,PORT,IP:PORT或IP/PORT及协议进行筛选,同时可以查阅当前所有捕获的数据包(如果没有设置捕获前过滤,否则只能查阅过滤后的数据)。 二、使用说明:本应用的使用环境为Windows 7、Windows 8及Windows 8.1。 在使用过程中需要获得管理员权限 - 捕获数据包需要访问底层数据,需要获得最高权限才可以正常运行该应用; 三、作者留言 该应用的开发环境为VS2013,所用语言为C#,界面设计属于WinForm(比较老式的界面风格,推荐使用WPF)。因本人水平有限,在该应用中不免存在很多漏洞和不足;如果你有更多更好的想法或者发现该小应用中的bug还望批评指正。 ||联系方式:LHearen@126.com|| 四、免责声明 本系统仅用于学习交流之用,本人不承担该应用的技术及版权问题,且不对该应用负法律责任。
关于的源代码#include <winsock2.h> #include <windows.h> #include <ws2tcpip.h> #include <stdio.h> #include <stdlib.h> #pragma comment(lib,"ws2_32.lib") #define MAX_HOSTNAME_LAN 255 #define SIO_RCVALL _WSAIOW(IOC_VENDOR,1) #define MAX_ADDR_LEN 16 struct ipheader { unsigned char ip_hl:4; unsigned char ip_v:4; unsigned char ip_tos; unsigned short int ip_len; unsigned short int ip_id; unsigned short int ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short int ip_sum; unsigned int ip_src; unsigned int ip_dst; }; typedef struct tcpheader { unsigned short int sport; unsigned short int dport; unsigned int th_seq; unsigned int th_ack; unsigned char th_x:4; unsigned char th_off:4; unsigned char Flags; unsigned short int th_win; unsigned short int th_sum; unsigned short int th_urp; }TCP_HDR; typedef struct udphdr { unsigned short sport; unsigned short dport; unsigned short len; unsigned short cksum; }UDP_HDR; void main(){ SOCKET sock; WSADATA wsd; DWORD dwBytesRet; unsigned int optval = 1; unsigned char *dataudp,*datatcp; int i,pCount=0,lentcp, lenudp; SOCKADDR_IN sa,saSource, saDest; struct hostent FAR * pHostent; char FAR name[MAX_HOSTNAME_LAN]; char szSourceIP[MAX_ADDR_LEN], szDestIP[MAX_ADDR_LEN],RecvBuf[65535] = {0}; struct udphdr *pUdpheader; struct ipheader *pIpheader; struct tcpheader *pTcpheader; WSAStartup(MAKEWORD(2,1),&wsd); if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP))==SOCKET_ERROR) exit(1); gethostname(name, MAX_HOSTNAME_LAN); pHostent = gethostbyname(name); sa.sin_family = AF_INET; sa.sin_port = htons(6000); memcpy(&sa.sin_addr.S_un.S_addr, pHostent->h_addr_list[0], pHostent->h_length); bind(sock, (SOCKADDR *)&sa, sizeof(sa)); if ((WSAGetLastError())==10013) exit(1); WSAIoctl(sock, SIO_RCVALL, &optval, sizeof(optval), NULL, 0, &dwBytesRet, NULL, NULL); pIpheader = (struct ipheader *)RecvBuf; pTcpheader = (struct tcpheader *)(RecvBuf+ sizeof(struct ipheader )); pUdpheader = (struct udphdr *) (RecvBuf+ sizeof(struct ipheader )); while (1){ memset(RecvBuf, 0, sizeof(RecvBuf)); recv(sock, RecvBuf, sizeof(RecvBuf), 0); saSource.sin_addr.s_addr = pIpheader->ip_src; strncpy(szSourceIP, inet_ntoa(saSource.sin_addr), MAX_ADDR_LEN); saDest.sin_addr.s_addr = pIpheader->ip_dst; strncpy(szDestIP, inet_ntoa(saDest.sin_addr), MAX_ADDR_LEN); lentcp =(ntohs(pIpheader->ip_len)-(sizeof(struct ipheader)+sizeof(struct tcpheader))); lenudp =(ntohs(pIpheader->ip_len)-(sizeof(struct ipheader)+sizeof(struct udphdr))); if((pIpheader->ip_p)==IPPROTO_TCP&&lentcp!=0){ printf("*******************************************\n"); pCount++; datatcp=(unsigned char *) RecvBuf+sizeof(struct ipheader)+sizeof(struct tcpheader); printf("-TCP-\n"); printf("\n%s\n",szDestIP); printf("\n%i\n",ntohs(pTcpheader->dport)); printf("datatcp address->%x\n",datatcp); printf("size of ipheader->%i\n",sizeof(struct ipheader)); printf("size of tcpheader->%i\n",sizeof(struct tcpheader)); printf("size of the hole packet->%i\n",ntohs(pIpheader->ip_len)); printf("\nchar Packet%i [%i]=\"",pCount,lentcp-1); for (i=0;i<lentcp;i++){ printf("\\x%.2x",*(datatcp+i)); if (i==0) printf("\"\n\""); } printf("\";\n\n\n"); for (i=0;i<lentcp;i++){ if( *(datatcp+i)<=127&&*(datatcp+i)>=20) printf("%c",*(datatcp+i)); else printf("."); } printf("\n\n*******************************************\n"); } if((pIpheader->ip_p)==IPPROTO_UDP&&lentcp!=0){ pCount++; dataudp=(unsigned char *) RecvBuf+sizeof(struct ipheader)+sizeof(struct udphdr); printf("-UDP-\n"); printf("\n%s\n",szDestIP); printf("\n%d\n",ntohs(pTcpheader->dport)); printf("UDP%x\n",dataudp); printf("IP%i\n",sizeof(struct ipheader)); printf("UDP%i\n",sizeof(struct udphdr)); printf("%i\n",ntohs(pIpheader->ip_len)); printf("\nchar Packet%i [%i]=\"",pCount,lenudp-1); for (i=0;i<lenudp;i++){ printf("\\x%.2x",*(dataudp+i)); if (i==0) printf("\"\n\""); } printf("\";\n\n\n"); for (i=0;i<lenudp;i++){ if( *(dataudp+i)<=127&&*(dataudp+i)>=20) printf("%c",*(dataudp+i)); else printf("."); } printf("\n\n*******************************************\n"); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值